Allow entity instances to be tracked by two bounded contexts, but limit updates to one context
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Review the DbContext model configuration, entity tracking, the documented migration-exclusion API, and the per-query AsNoTracking approach. Done would be a supported model-level way for DbContext B to read and join User without tracking or inserting it, while DbContext A retains create, update, and delete responsibility.
Written by the indexing model from the issue text.
Description
Ask a question
I am using bounded contexts.
This means I have two different DbContexts, with their own entities, and own "Schema", but sharing the same connection string.
My question comes where there is a relationship between the boundaries.
In DbContext A there is a User Entity.
In DbContext B there is a need to "join" some Foo entity to the User entity - so I want to model this as a navigation property (from Foo --> User).
I have found that I can include the User entity into the model for DbContext B - and ignore it for migration purposes as documented here:
https://docs.microsoft.com/en-us/ef/core/modeling/entity-types?tabs=data-annotations#excluding-from-migrations
However, I want to make it explicit that this User entity should only be tracked in DbContext A, and not tracked in DbContext B. This is because responsibility for the Create, Update and Delete of the User entity should only ever be performed in DbContext A. DbContext B just needs the ability to read and join to this entity in it's model.
I have seen from here: https://stackoverflow.com/questions/9415334/entity-framework-code-first-readonly-entity that you can use AsNoTracking() on a per query level to do this. However is there anything I can do at the model level - similar to the exclude migration api I linked above, that tells DbContext B to never track this entity.
The issue I am trying to avoid is this:
- In my application I use some service that creates and adds a User entity to DbContext A.
- I then use another service that creates a
Fooentity in DbContext B, and sets a navigation property on theFooentity, to theUserentity thats already been added to DbContext A, and was not tracked previously by DbContext B. - I call SaveChanges() on DbContext A which inserts the User entity.
- I call SaveChanges() on DbContext B, which, sees the navigation property on the
Fooentity to theUserentity - and also tries to insert a new User entity.
Note: I haven't actually tested this scenario yet but I am assuming this problem would occur based on my current understanding of EF Core and how it wants to track entities. Between steps 3 and 4, the User entity will have been inserted but DbContext B will not know that - despite the User entity has now been assigned an Id by DbContext A after the insert.
Rather than addressing this on a per-query basis, I'd like to know if there is something I can do at a model level so better isolate entities to prevent them from being tracked in contexts that should not "own" them.
Include provider and version information
EF Core version: 5.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer): Microsoft.EntityFrameworkCore.SqlServer
Target framework: (e.g. .NET 5.0): .NET 5.0
Operating system: Windows 10
IDE: (e.g. Visual Studio 2019 16.3): Visual Studio 2019 Pro 16.8.2
- 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