Open Uriil opened 4 months ago
The issue here is that the ReferenceId
property is being forced into being a foreign key for two different relationships. @AndriySvyryd Given that you assigned this to me, I assume you are saying this is a valid model and fixup should work correctly?
@AndriySvyryd Given that you assigned this to me, I assume you are saying this is a valid model and fixup should work correctly?
No, it just means that I think you would be a good choice to investigate this. We can still decide that this isn't a supported scenario.
@AndriySvyryd Do you think we should allow creating a table where the same FK is constrained to two different FKs on two different tables?
Table: CustomAttribute
PK_CustomAttribute {'Id'} PrimaryKey
EntityTypeMappings:
CustomAttribute - CustomAttribute
Columns:
Id (int) NonNullable)
ReferenceId (int) NonNullable)
Type (nvarchar(max)) NonNullable)
Value (int) NonNullable)
ForeignKeyConstraints:
FK_CustomAttribute_ChildEntity_ReferenceId CustomAttribute {'ReferenceId'} -> ChildEntity {'Id'} Cascade
FK_CustomAttribute_Entity_ReferenceId CustomAttribute {'ReferenceId'} -> Entity {'Id'} Cascade
Indexes:
IX_CustomAttribute_ReferenceId {'ReferenceId'}
If so, we'll have to handle this in change tracking, which would be non-trivial.
Two FKs on the same property pointing to different tables means that there should be a principal row in each and therefore both Entity and ChildEntity for a given key value will have the same collection of attributes. The behavior described in the first comment is consistent with this, though the database state is not, that data could only have been inserted if the FKs were not enforced.
Having said that, I don't think this behavior is useful, unless the principals are in the same hierarchy using TPT, but in that case only the base type would have the collection. So, in conclusion we should just throw in model validation for this.
We are using EF Core 8.0.6 and observe strange reference assignment when trying to save hierarchy of objects. We have next structure: Entity, Child Entity and CustomAttribute. Entity has multiple CustomAttributes and multiple ChildEntities. ChildEntity has multiple CustomAttributes. CustomAttribute for both Entity and ChildEntity is the same class. Whenever we save hierarchy of objects EF Core merges CustomAttributes for Entity and FIRST(literally first) ChildEntity. Please see code below and result
And result in database:
Entity and ChildEntity tables are good, problem in CustomAttribute. There is corruption in CustomAttribute for first child entity(record with Id 1 on above screenshot), it has value from first ChildEntity, but id of parent Entity
What's more, problem occurs on adding entity to context, not on saving it, looks like Entity and FIRST ChildEntity reference same merged collection of CustomAttributes:
Expected results
Workarounds which work
Include provider and version information
EF Core version: 8.0.6 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 8.0) Operating system: Windows 11 IDE: (e.g. Visual Studio 2022 17.10.1)