最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Possible to use a variable entity type or workaround? - Stack Overflow

programmeradmin3浏览0评论

I have a function I call in OnModelCreating lots of times, I'm trying to see if I can just have this happen automatically.

The WithProjection function is being called for properties that are a class type that inherits from a particular class, so it's easy enough to distinguish them, so I already have a list of my desired properties. Instead of manually defining these in my application's OnModelCreating, I was hoping to just be lazy:

var models = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes())
    .Where(t => t.IsSubclassOf(typeof(ModelBase)));

foreach (var model in models)
{
    var modelProperties = model.GetProperties();

    foreach (var modelProperty in modelProperties)
    {
        Type modelPropertyType = modelProperty.PropertyType;

        //Some irrelevant filtering logic here

        builder.Entity<modelPropertyType>() //doesn't work because modelPropertyType is a var
           .WithProjection() // <-- my custom function
    }
}

Builder has an alternate Entity function that takes a type as a parameter, but it returns a generic EntityTypeBuilder object, not a typed one, which tracks but means my custom function doesn't work as it's attached to the typed object.

I think my only option to make this work is to recreate my entire custom function to operate on the untyped EntityTypeBuilder, but before I do, I'm asking you.

Is there a way to get a typed EntityTypeBuilder here?

发布评论

评论列表(0)

  1. 暂无评论