15

In EF6 we could do the following:

context.Set<TEntity>().Attach(entity); context.Entry(entity).Collection("NavigationProperty").Load(); 

Since EF Core is going "100% strictly typed", they have removed the Collection function. What should we use instead?

How do I load navigation properties for an ATTACHED entity? That is, do I have a way to invoke something like .Include() and .ThenInclude(), on an entity I have already retrieved?

1
  • 1
    This API is coming back in EF Core 1.1 Commented Oct 17, 2016 at 15:33

3 Answers 3

24

Explicit Loading was added in Entity Framework Core v1.1. See Microsoft Docs

From the docs:

using (var context = new BloggingContext()) { var blog = context.Blogs .Single(b => b.BlogId == 1); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog) .Reference(b => b.Owner) .Load(); } 
Sign up to request clarification or add additional context in comments.

Comments

22

You have 3 methods:

1. Eager loading

e.g.

var blogs = context.Blogs .Include(blog => blog.Posts) .ToList(); 

2. Explicit loading

e.g.

var blog = context.Blogs .Single(b => b.BlogId == 1); context.Posts .Where(p => p.BlogId == blog.BlogId) .Load(); 

3. Lazy loading (as of EF Core 2.1)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseLazyLoadingProxies() .UseSqlServer(myConnectionString); 

You can read more about it here : Loading Related Data

Update :

You can use TrackGraph API for that use case.Here is the link : graph behavior of Add/Attach

Another link : DbSet.Add/Attach and graph behavior

8 Comments

Two methods? THAT IS NOT THE SAME since we discuss the the scenario where entity is attached and this is important. Such scenario is used where entity is "created" from HTTP Request Form fields and you need to merge its Many-To-Many references (for this you should download the current state) and then find what to delete.
please see the Update.
Thank you for your comments and links. I see that there is a sense in changing tracking changes behavior DURING ATTACH but can't understand how it is related to blocking possibility to load navigation property for ALREADY ATTACHED entity. May be you have your own thoughts on this?
@RomanPokrovskij If you care to read the first provided link, specifically Explicit Loading section which corresponds to the EF6 feature you are asking, you'll notice the very first statement Explicit loading does not yet have a first class API in EF Core. and a link to their backlog tracking this feature. So it's not a trimmed down feature, it's just not implemented yet. The link also contains a workaround (copy/pasted by Sampath), all you need is to understand it and utilize it for your use case.
@RomanPokrovskij Lol, I see your point:) Well, English is not my natural language, to me it just sounds "we don't support that yet natively, here is your workaround blah-blah (and thank you for being our alpha tester)" :)
|
1

See below Code,
I'm inserting data in UserRef table as well another table which we have many 2 many relationships.

public void AddUser(User user, IEnumerable<UserSecurityQuestion> securityQuestion, string password) { var userModel = _mapper.Map<User, UserRef>(user); userModel.CreateTime = DateTime.Now; userModel.UserNewsLetterMaps.ToList().ForEach(u => this._context.UserNewsLetterMaps.Add(u)); this._context.RoleRefs.Attach(new RoleRef() { RoleId = (int)user.UserRole, UserRefs = new List<UserRef> { userModel } }); userModel.ResidenceStatusRefs.ToList().ForEach(u => this._context.ResidenceStatusRefs.Attach(u)); this._context.UserRefs.Add(userModel); this.Save(); } 

2 Comments

Yes something like this should work: load entity from context and then somehow tweak "navigationPropertis" between two entities "attached" and that new "get from context"... If I understand the code right... But this is so ugly and so sad...
It seems I got that "explicit loading" should work for attached entities to. Should experiment with this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.