EF 4.1 SaveChanges不更新导航或引用属性 [英] EF 4.1 SaveChanges not updating navigation or reference properties

查看:328
本文介绍了EF 4.1 SaveChanges不更新导航或引用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码,可以添加记录没有问题。数据库已正确创建,无任何问题播种。但是在编辑操作中调用SaveChanges()仅更新实体,而不是任何导航或引用属性。



我的模型的简化版本:

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

public int Id {get; set;}

public string Name {get; set;}

public bool删除{get; set;}

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

public virtual USState USState {get;组; }

}

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

public int Id {get; set;}

public string Name {get; set;}

public virtual ICollection< Contact>联系人{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()
{
}

//导航属性
public virtual ICollection< Contact>联系人{get;组; }
}

我不会包含代码,但我确实有一些自定义模型约束力我的编辑操作(使用MVC 3)没有填充ContactType或USState属性。绑定正确地返回一个填充完整的联系人对象,并从表单中输入正确的值。



如果我正确理解EF 4.1,我只需要执行以下操作即可保存我的联系人对象时更改数据库:

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

但是,只有原始属性正在更新。我在上面的例子中已经省略了一些事情:我正在使用一个事务,一个try catch块,并检查它是否是一个新的记录。



我已经使用SQL Profiler,并且可以确认将ContactType和Territory表的更新查询发送到数据库服务器。



状态属性我的导航和参考属性已修改,但我也尝试手动设置:

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

我可能会误会,但我确定我的代码在CTP5下工作。 >

任何想法?在这一点上,我不知道我应该如何调试这个。



谢谢,Steve

解决方案

尝试这样:

  context.Entry(contact.USState).State = EntryState.Modified ; 


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

解决方案

Try this:

  context.Entry(contact.USState ).State = EntryState.Modified;

这篇关于EF 4.1 SaveChanges不更新导航或引用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆