【发布时间】:2011-06-07 15:40:42
【问题描述】:
我已经为这个问题苦苦挣扎了一段时间了。使用 UpdateModel() 时,我似乎无法正确更新实体对象。
我只是不认为这是一个复杂的数据模型。看起来这应该是一个非常普遍的情况。也许我需要在 fluent api 的上下文中添加一些内容来消除此错误,但我无法终生解决。
错误信息
操作失败:无法更改关系,因为一个或多个外键属性不可为空。当对关系进行更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
这是我的模型和上下文:
联系方式:
public class Contact {
public Contact() {
this.ContactInformations = new HashSet<ContactInformation>();
}
public int ContactId { get; set; }
[Required]
public string Firstname { get; set; }
[Required]
public string Lastname { get; set; }
public virtual ICollection<ContactInformation> ContactInformations { get; set; }
}
联系方式:
public class ContactInformation {
public int ContactInformationId { get; set; }
public int ContactTypeId { get; set; }
public int ContactId { get; set; }
[Required]
public string Information { get; set; }
}
数据库上下文:
public class Database : DbContext {
public Database()
: base("Contacts") {
}
public DbSet<Contact> Contacts { get; set; }
public DbSet<ContactInformation> ContactInformations { get; set; }
}
联系人。
public ActionResult Edit(int id, Contact form) {
var contact = db.Contacts.SingleOrDefault(c => c.ContactId == id);
UpdateModel(contact);
db.SaveChanges();
return RedirectToAction("Index");
}
形成数据
[0] "ContactId"
[1] "Firstname"
[2] "Lastname"
[3] "ContactInformations[0].Index"
[4] "ContactInformations[0].ContactInformationId"
[5] "ContactInformations[0].ContactId"
[6] "ContactInformations[0].Information"
[7] "ContactInformations[0].ContactTypeId"
[8] "ContactInformations[1].Index"
[9] "ContactInformations[1].ContactInformationId"
[10] "ContactInformations[1].ContactId"
[11] "ContactInformations[1].Information"
[12] "ContactInformations[1].ContactTypeId"
更新 与我的问题有关的一些有趣细节Here
【问题讨论】:
-
我的 0.02 美元:不要直接绑定到 EF 实体。使用编辑/查看模型。我知道,这不是您要问的,但这最终会使这个问题变得无关紧要。
-
@Craig Stuntz 我在做任何可以使这段代码正常工作的事情上都没有问题。这真是一个黑客,甚至发布这个问题都令人尴尬。如果我使用视图模型,您如何建议将表单中的 ContactInformation 与正在编辑的联系人的联系信息同步?我愿意接受任何建议。
-
好吧,在我们走这条路之前,我想到了另一个想法:你在数据库中有级联,级联映射是否进入了你的模型?见:stackoverflow.com/questions/2477872/…
-
@Craig Stuntz 我没有使用任何类型的设计师。我首先使用实体框架 4.1 代码,所以我所有的模型对象都只是 poco 类。我可以使用 fluent API,但由于使用了约定,所以到目前为止我还不需要。
-
好的,但级联问题仍然相关。您的数据库和模型中有级联吗?
标签: c# asp.net-mvc entity-framework ef-code-first