Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!