开发者

Entity framework auto eager load

Is there a way to get entity objects to automatically pull all relevant data through the relationships instead of having having to .Include everything I want populated? I can't use lazy loading as this need开发者_C百科s to be serialized to go over WCF. It would just be fantastic to have it auto populate the relevant collections.


No there is no build in feature which will automatically eagear load whole object graph. You must always explicitly say which navigation properties you want to eager load (by using Include method or LoadProperty method).

Edit:

Based on your comment: Generally it should be possible to build some auto loader - but it will not be easy. ObjectContext has MetadataWorkspace property which contains all metadata about your entities. You can load all information about your entities from metadata and add needed inclueds to the query. I expect one problem - you must somehow handle cyclic references. If you need some example how to extract information about entities check T4 POCO generation template.


I came across this querstion recently because I was looking for something similar.

Since EF Core 6 there is a AutoInclude method that configures whether a navigation should be included automatically.

This can be done in the OnModelCreation method in the DbContext class:

modelBuilder.Entity<Theme>().Navigation(e => e.ColorScheme).AutoInclude();

This would load the ColorScheme for every Theme when running the query:

var themes = context.Themes.ToList();

See Model configuration for auto-including navigations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜