Don't fail silently when meeting data that breaks a defined relationship
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reproducing the one-to-one relationship with duplicate foreign-key values and observe how EF Core marks duplicates in the change tracker. Check the behavior before and after SaveChangesAsync; done should mean inconsistent relationship data is not silently treated as deleted, with the resulting handling made explicit.
Written by the indexing model from the issue text.
Description
I'll explain with an example (which is how I learnt of this):
Let's say you define a one-to-one relationship between data in two tables, such as below.
entity.HasOne(typeof(User), nameof(BaseEntity.UserCreated))
.WithOne()
.HasForeignKey(entityType.ClrType, nameof(BaseEntity.UserCreatedID));
entity.HasOne(typeof(User), nameof(BaseEntity.UserUpdated))
.WithOne()
.HasForeignKey(entityType.ClrType, nameof(BaseEntity.UserUpdatedID));
You then retrieve a collection of the data containing the foreign key, and it turns out multiple entities in that table has the same foreign-key. The way EF Core seems to handle this is to mark duplicates as deleted in the change tracker, and then it moves on.
It makes sense that this doesn't work as expected, as the relationships defined in EF Core doesn't match the real data. BUT it shouldn't fail silently, and it definitely shouldn't just track 'duplicates' as deleted. One call to DBContext.SaveChangesAsync(), and that data is gone.
I'm not sure if the appropriate response would be to throw an exception, but I think it's wrong to just manipulate the data so that the relationships are still true. Very open to suggestions about how such a situation may be handled better.
I obviously got into this situation because I mistakenly defined my relationship as a one-to-one relationship instead of one-to-many. But it'd be nice if EF Core helped me discover that mistake, instead of silently 'fixing' the issue.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100