dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.66k stars 3.16k forks source link

AutoInclude via data annotations #28486

Open LukasKubicek opened 2 years ago

LukasKubicek commented 2 years ago

AutoInclude is a not much documented, but very useful feature since ef core 5, that allows you to load related entities automatically with model builder like this:

builder.Navigation(x => x.ContactEmails).AutoInclude();

I would like to be able to configure this by data annotaions on the model like this:

[AutoInclude]
public ICollection<Email> ContactEmails {get; set;}
roji commented 2 years ago

You may want to consider making Email an owned entity.

smitpatel commented 2 years ago

Owned entities have a lot of other implications and limitations. Making an entity owned for the sake of auto-including is pit of failure.

LukasKubicek commented 1 year ago

I would like to promote this again. The AutoInclude feature is very useful, but it is unfortunate that it is not available via DataAnnotations on Model classes directly.

pawchen commented 9 months ago

Yes, another example is the Author for a Topic and a Reply connected by AuthorId.