Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get entity navigation properties after insert

If I understand you correctly, you're trying to eagerly load a complex property after establishing a relationship via a foreign key property.

SaveChanges() does not do anything in the way of loading complex properties. At most, it is going to set your primary key property if you're adding new objects.

Your line reward = context.Set<Reward>().SingleOrDefault(a => a.Id == reward.Id); also does nothing in the way of loading Campaign because your reward object is not attached to the context. You need to explicitly tell EF to load that complex object or attach it then let lazy loading work its magic.

So, after you context.SaveChanges(); you have three options for loading reward.Campaign:

  1. Attach() reward to the context so that Campaign can be lazily loaded (loaded when accessed)

     context.Rewards.Attach(reward);
    

    Note: You will only be able to lazy load reward.Campaign within the context's scope so if you're not going to access any properties within the context lifespan, use option 2 or 3.

  2. Manually Load() the Campaign property

     context.Entry(reward).Reference(c => c.Campaign).Load();
    

    Or if Campaign was a collection, for example Campaigns:

     context.Entry(reward).Collection(c => c.Campaigns).Load();
    
  3. Manually Include() the Campaign property

     reward = context.Rewards.Include("Campaigns")
         .SingleOrDefault(r => r.Id == reward.Id);
    

    Although, I'd suggest Load since you already have reward in memory.

Check out the Loading Related Objects Section on this msdn doc for more information.


As you are creating your reward object as new Reward(), EF doesn't have a proxy. Instead, create it using DbSet.Create like this:

var reward = context.Set<Reward>().Create();
reward.CampaignId = 5;
context.SaveChanges();

Next attach it to your DbSet:

context.Rewards.Attach(reward);

Finally, you can now use lazy loading to get related entities:

var campaign = reward.Campaign;

I have a simple Solution around the problem.

instead of adding the CampaignID to the reward, add the campaign Object.. so:

var _campaign = context.Campaign.First(c=>c.Id == 1);//how ever you get the '1'
var reward = new Reward { Campaign = _campaign };
context.Set<Reward>().Add(reward);
context.SaveChanges();

//reward.Campaign is not null

Entity framework does all the heavy lifting here.

You're probably thinking that it's a waste to load the entire Campaign object but if you are going to be using it (from what it looks like, seems you are) then I don't see why not. You can even use the include statement when fetching it above if you need to access navigation properties from the Campaign object...

var _campaign = context.Campaign.include(/*what ever you may require*/).First(c=>c.Id = 1);

In addition to Carrie Kendall and DavidG (in VB.NET):

Dim db As New MyEntities
Dim r As Reward = = db.Set(Of Reward)().Create()
r.CampaignId = 5
db.Reward.Add(r) ' Here was my problem, I was not adding to database and child did not load
db.SaveChanges()

Then, property r.Campaign is available