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?