Get entity navigation properties after insert

前端 未结 6 791
花落未央
花落未央 2020-12-23 02:14

I have the following 2 classes:

public class Reward 
{
    public int Id { get; set; }
    public int CampaignId { get; set;
    public virtual Campaign Camp         


        
相关标签:
6条回答
  • 2020-12-23 02:56

    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

    0 讨论(0)
  • 2020-12-23 02:59

    Did you try using Include()? Something like this:

    reward = context.Set<Reward>().Include("Campaigns").SingleOrDefault(a => a.Id == reward.Id);
    
    0 讨论(0)
  • 2020-12-23 02:59

    if you have more than one navigation properties or want to add more than one records it may be hard to do it in these ways.

    so i suggest if the memory doesn't matter create a new context object after adding your records , and use it instead

    0 讨论(0)
  • 2020-12-23 03:10

    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;
    
    0 讨论(0)
  • 2020-12-23 03:10

    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);
    
    0 讨论(0)
  • 2020-12-23 03:13

    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.

    0 讨论(0)
提交回复
热议问题