Skip to content

Document readonly/IEnumerable navigation patterns. #3668

Closed

Description

@Zefek

Hello,

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:

public void AddItem(int price)
{
    var oldItems = new List<InvoiceItem>(Items);
    oldItems.Add(new InvoiceItem(price));
    Items = oldItems.;
}

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:

  1. How to map backing field for it?
  2. 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

  1. ajcvickers commented on Jan 10, 2022

    @ajcvickers
    Contributor

    @Zefek You can do something like this:

    public class Blog
    {
        private readonly List<Post> _posts = new();
    
        public int Id { get; set; }
    
        public IEnumerable<Post> Posts => _posts;
    
        public void AddPost(Post post) => _posts.Add(post);
    }

    The backing field _posts is found automatically. Include works fine--the navigation is still Posts, 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.

  2. changed the title [-][/-] [+][/+] on Jan 10, 2022
  3. transferred this issue fromdotnet/efcoreon Jan 10, 2022
  4. Zefek commented on Jan 11, 2022

    @Zefek
    Author

    @ajcvickers Thank you very much. This is exactly what I needed to know :)

  5. self-assigned this
    on Jan 11, 2022
  6. added this to the 7.0.0 milestone on Jan 11, 2022
  7. added 8 commits that reference this issue on Feb 25, 2023
    e20432f
    671a66c
    94ff962
    f33e5ed
    c7b38aa
    93b4c5e
    1ba858c
    bb39d2d
  8. added a commit that references this issue on Mar 19, 2023
    9a1e6f2
  9. added 2 commits that reference this issue on Mar 28, 2023
    f5d7299
    dd7c736
  10. modified the milestones: 7.0.0, 8.0.0 on Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions