You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to ask about collection in EF Core which are used to relations. For example I have entity Invoice and InvoiceItem. Invoice implements public ICollection<InvoiceItem> Items {get; set;} property.
But ICollection exposes Add method so client can call invoice.Items.Add(new InvoiceItem(...)); It is violation of domain model rule that only aggregate root can manage entities inside aggregate.
When I change implementation to IEnumerable(IReadOnlyCollection)<InvoiceItem> there is not Add method so client cannot add new InvoiceItem to Invoice. But Invoice entity cannot too. There would have to be Add method on Invoice:
Question is how EF tracker will be worked that collection is new object.
I can add private field private List<InvoiceItem> items = new List<InvoiceItems>(); Then I change public IEnumerable<InvoiceItem> Items => items;
There are two question:
How to map backing field for it?
How will work Include method ... dbContext.Invoices.Include(k=>Items)?
Second problem is that I have public setter on InvoiceItem. Can EF work with private set method on navigation property?
Is there any possibility how to work with collections to satisfy domain model rule that entities in aggregate can manage only root entity?
Thank you.
Activity
ajcvickers commentedon Jan 10, 2022
@Zefek You can do something like this:
The backing field
_postsis found automatically. Include works fine--the navigation is stillPosts, but EF uses the backing field to interact with it.See https://blog.oneunicorn.com/2016/10/28/collection-navigation-properties-and-fields-in-ef-core-1-1/ for more information.
[-][/-][+][/+]Zefek commentedon Jan 11, 2022
@ajcvickers Thank you very much. This is exactly what I needed to know :)
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation