Closed sylvaingirardbe closed 1 year ago
Initial observation - Address is owned entity (only Company has Address). Communication is weak entity (Person.Communication & Company.Communication)
@sylvaingirardbe To add a little more to what @smitpatel said it's probably worth saying a bit about when DefiningEntityType
is needed. For a given CLR type, if that CLR type is only owned once by one owning entity type, then there is still a 1:1 mapping between CLR type and conceptual entity type. This is the case for Address--it is owned only once by Company. This is also the normal case for non-owned entity types.
However, if the CLR type is owned more than once, then there needs to be a different conceptual entity type in the model for each way the CLR type is used. DefiningEntityType
is part of the mechanism that differentiates these different conceptual entity types from each other. This is the case for Communication--it is owned once by Person and once by Company.
So it’s correct to say that DefiningEntityType
Is not a reliable way to determine if a type is owned? Is there a reliable way except for doing some reflection to get that attribute?
entityType.HasDefiningNavigation() || entityType.IsOwned()
entityType.IsOwned()
is enough
I'm using
modelBuilder.Model.GetEntityTypes()
to find types that are not owned and set their table name to the entity name to prevent pluralization, except for owned types to prevent table splitting. When checkingmodelBuilder.Model.GetEntityTypes()
,Commication
has itsDefiningEntityType
correctly set toCompany
whileAddress
hasn't while both are defined as owned types ofCompany
.Steps to reproduce
I have an entity
Address
and an entity
Communication
both owned by an entity
Company
Company
configurationDebug view of the model:
Further technical details
EF Core version: 3.1.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 3.1 Operating system: Windows 10 IDE: Rider