gpt4 book ai didi

c# - CTP5 EF代码第一题

转载 作者:太空狗 更新时间:2023-10-29 21:47:10 24 4
gpt4 key购买 nike

您可以找到演示此问题的源代码@ http://code.google.com/p/contactsctp5/

我有三个模型对象。联系人、联系人信息、联系人信息类型。一个联系人有很多联系人信息,每个联系人信息都是一个联系人信息类型。我猜很简单。我遇到的问题是当我去编辑联系人对象时。我从我的联系人存储库中提取它。然后我运行“UpdateModel(contact);”并使用我表单中的所有值更新对象。 (使用调试进行监控)当我保存更改时,出现以下错误:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

似乎在我调用更新模型后它使我的引用无效,这似乎破坏了一切?任何关于如何补救的想法将不胜感激。谢谢。

这是我的模型:

public partial class Contact {
public Contact() {
this.ContactInformation = new HashSet<ContactInformation>();
}

public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

public virtual ICollection<ContactInformation> ContactInformation { get; set; }
}

public partial class ContactInformation {
public int ContactInformationId { get; set; }
public int ContactId { get; set; }
public int ContactInfoTypeId { get; set; }
public string Information { get; set; }

public virtual Contact Contact { get; set; }
public virtual ContactInfoType ContactInfoType { get; set; }
}

public partial class ContactInfoType {
public ContactInfoType() {
this.ContactInformation = new HashSet<ContactInformation>();
}

public int ContactInfoTypeId { get; set; }
public string Type { get; set; }

public virtual ICollection<ContactInformation> ContactInformation { get; set; }
}

我的 Controller 操作:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Contact person) {
if (this.ModelState.IsValid) {
var contact = this.contactRepository.GetById(person.ContactId);
UpdateModel(contact);
this.contactRepository.Save();
TempData["message"] = "Contact Saved.";
return PartialView("Details", contact);
} else {
return PartialView(person);
}
}

上下文代码:

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {
modelBuilder.Entity<Contact>()
.HasMany(c => c.ContactInformation)
.WithRequired()
.HasForeignKey(c => c.ContactId);

modelBuilder.Entity<ContactInfoType>()
.HasMany(c => c.ContactInformation)
.WithRequired()
.HasForeignKey(c => c.ContactInfoTypeId);
}

最佳答案

这里发生了一些事情。

1 如果您设置为延迟加载,则只有在您告诉它们加载时才会加载子对象。这可以通过您的查询中的以下内容来完成。

..

context.Contacts.Include(c => c.ContactInfos).Include(c => c.ContactInfos.ContactInfoType)

参见 this article有关确保按需要加载对象的完整详细信息。

2 如果您不想保存 contactinfo 和 contactinfotype(因为它们未加载或您只是不想保存),您需要告诉上下文不要保存子对象那不应该更新。这可以通过以下方式完成:

..

context.StateManager.ChangeObjectState(entity.ContactInfos.ContactInfoType, EntityState.Unchanged);

我发现在将国家/地区对象更改/使用到用户数据时我需要这样做。我绝对不希望用户更新它。

正在为这一切编写一些指南,但可能需要数周才能在我的 blog 上完成

3 MVC 不会存储/发回您未放入表单中的任何内容。如果您将对象层次结构发送到表单并且值未在隐藏输入中表示,它们将在您的模型中返回为空。出于这个原因,我通常制作的 View 模型只能是实体的可编辑版本,上面有 ToEntity 和 ToModel 方法。这也涵盖了我的安全性,因为我不希望隐藏输入中的各种用户 ID,只是为了让我的实体直接映射到 MVC(参见 this article on overposting)。

我本以为如果您将 contactinfo 属性设置为虚拟,UpdateModel 不会介意它们在返回时不存在,但我很可能是错的,因为我没有尝试过。

关于c# - CTP5 EF代码第一题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5208674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com