I'm doing a new API in .NET 6 with a custom dbcontext because I need to use Shadow Properties, wherever i'm reading a XML file where I store the information like this.
<?xml version="1.0" encoding="UTF-8" ?>
<Container>
<Properties>
<Property>
<Entity>Enterprise</Entity>
<Name>Actor</Name>
<TypeEntity>String</TypeEntity>
</Property>
</Properties>
</Container>
And I need to use the "Property" to build shadow properties in my model like this:
protected override void OnModelCreating(ModelBuilder builder)
{
string path = Path.Combine(Environment.CurrentDirectory, @"GraphQL\CustomProperties", "Properties.xml");
XDocument doc = XDocument.Load(path);
foreach (XElement element in doc.Descendants("Properties").Descendants("Property"))
{
var Entity = element.Element("Entity").Value;
var CustomType = element.Element("TypeEntity").Value;
builder.Entity<Foo>().Property<string>(element.Element("Name").Value).IsRowVersion();
}
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
I need to get "CustomType" and send it into parameter Property like this:
builder.Entity<Foo>().Property<CustomType>(element.Element("Name").Value).IsRowVersion();
But when I do this appears the next message: "CustomType Is a variable but is used like a type" Any solution? Thanks!
There's another overload for that Property method you're invoking that takes a Type argument instead of a generic argument.
You'll need to find the System.Type that corresponds to the value of your CustomType, and then call that overload.
var customTypeName = element.Element("TypeEntity").Value;
var customType = customTypeName switch {
"String" => typeof(string), // add other possibilities here.
};
builder.Entity<Foo>().Property(customType, element.Element("Name").Value).IsRowVersion();