Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Pass variable as Type c#

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!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda