In the OnModelCreating of MyDbContext I am having a piece of code doing the following.
modelBuilder.Types()
.Having(t => t.IsConcreteImplementationOf(typeof(MyClass<>)) ? t : null)
.Configure((c, t) =>
{
c.IsComplexType();
//...
});
I tend to do some custom mapping for properties of MyClass< T >, which actually works quite fine, as long as there is a none generic class inheriting it. (I am leaving out the details about MyClass and the custom mapping)
So: it works fine if there is
class MyClassString : MyClass<String> {}
class MyEntity {
MyClassString MyProperty { get; set; }
}
However, it does not work when I am just writing
class MyEntity {
MyClass<String> MyProperty { get; set; }
}
I totally understand that EF can not configure generic types out of the box (it has to call Configure for each value of T. so it has to know which values are used for T, it can't configure them all...).
I tried
modelBuilder.RegisterEntityType(typeof(MyClass<String>));
but this is giving the error:
The type 'EntityField`1' has already been configured as an entity type. It cannot be reconfigured as a complex type.
Which is actually quitte promising :-) It means that MyClass< String > is registered as an Entity and has been provided for configuration.
I guess I basically need to do
modelBuilder.RegisterComplexType(typeof(MyClass<String>));
Unfortunately, this method does not exists :-(
How can this be done? I am totally fine with having a list of supported types for T in MyDbContext.
Aucun commentaire:
Enregistrer un commentaire