EF 4.1 SaveChanges not updating navigation or reference properties

你离开我真会死。 提交于 2019-12-04 16:05:43

问题


I'm using code first and can add records without a problem. The database is created properly and is seeded without any problems. But calling SaveChanges() in my Edit action only updates the entity, not any of the navigation or reference properties.

A simplified version of my model:

public class Contact : IMyBaseObject
{
    public Contact()
    {
       this.Delete = false;
       this.ContactTypes = new HashSet<ContactType>();
    }

    public int Id {get; set;}

    public string Name {get;set;}

    public bool Delete {get;set;}

    public virtual ICollection<ContactType> ContactTypes { get; set; }

    public virtual USState USState { get; set; }

}

public class ContactType : MyBaseObject
{
    public ContactType()
    {
    }

    public int Id {get; set;}

    public string Name {get;set;}

    public virtual ICollection<Contact> Contacts {get;set;}
}

public abstract class Territory : MyBaseObject
{
    public int Id {get; set;}

    public string Name {get;set;}

    public string Code {get;set;}
}

public class USState : Territory
{
    public USState()
    {
    }

    // Navigation properties
    public virtual ICollection<Contact> Contacts { get; set; }
}

I won't include the code, but I do have some custom model binding going on. My Edit action (using MVC 3) was not being populated with the ContactType or USState properties. The binding is properly returning a fully populated Contact object with the correct values from the form.

If I understand EF 4.1 properly, I should only have to do the following to persist changes to the database when saving my Contact object:

if(ModelState.IsValid)
{
    context.Entry(contact).State = EntryState.Modified;
    context.SaveChanges();
}

However, only primitive properties are being updated when I do that. I have left out a few things in the above example: I'm using a transaction, a try catch block, and checking if it's a new record.

I have used SQL Profiler and can confirm that the update queries for the ContactType and Territory tables are not being sent to the database server.

The State properties of my navigation and reference properties are Modified, but I have also tried manually setting that:

    context.Entry(contact).Collections("ContactTypes").EntityEntry.State = EntityState.Modified;
    context.Entry(contact).Reference("USState").EntityEntry.State = EntityState.Modified;

I could be mistaken, but I'm pretty sure my code was working under CTP5.

Any ideas? At this point, I'm not sure how I should approach debugging this.

Thanks, Steve


回答1:


Try this:

  context.Entry(contact.USState ).State = EntityState.Modified; //////



回答2:


Assuming your DAL objects/Collection are from Entity Framework, do this;

Contact.ContactTypes.Load();

This will refresh the related coltactTypes list from the underlying source.

Hope this helps.




回答3:


The code provided might not be enough. Better to see the code how the navigation properties were added in the code. Because even though the parent class was marked as modified, it doesn't mean the related navigation properties are marked as Added or modified as well. The quickest way is to turn on the debugger at where .SaveChanges was called, and then look at the context variable, look into "Local" properties, and then whether the navigation properties are there, also whether their modalstate was added or modified, then it will be much easier to tell what need to be done.




回答4:


am sure more code will help better determening the problem,especially the code for the model binding going on. but one thing i think you are already familiar with is that you need to access those navigation properties (Somehow) so EF will be able to automatically load them.because you marked your nav-properties as virtual you are allowing EF to Create proxies and override your nav-properties to allow Lazy Loading.



来源:https://stackoverflow.com/questions/5517182/ef-4-1-savechanges-not-updating-navigation-or-reference-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!