Estoy haciendo una nueva API en .NET 6 con un dbcontext personalizado porque necesito usar Shadow Properties, donde sea que esté leyendo un archivo XML donde almaceno la información como esta.
<?xml version="1.0" encoding="UTF-8" ?> <Container> <Properties> <Property> <Entity>Enterprise</Entity> <Name>Actor</Name> <TypeEntity>String</TypeEntity> </Property> </Properties> </Container>Y necesito usar la "Propiedad" para crear propiedades de sombra en mi modelo de esta manera:
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()); }Necesito obtener "CustomType" y enviarlo al parámetro Property de esta manera:
builder.Entity<Foo>().Property<CustomType>(element.Element("Name").Value).IsRowVersion();Pero cuando hago esto aparece el siguiente mensaje: "CustomType es una variable pero se usa como un tipo" ¿Alguna solución? ¡Gracias!
Hay otra sobrecarga para ese método de Property que está invocando que toma un argumento de Type en lugar de un argumento genérico.
Deberá encontrar el System.Type que corresponde al valor de su CustomType y luego llamar a esa sobrecarga.
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();