gpt4 book ai didi

c# - MVC : Updating One to Many Relationship

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:49 25 4
gpt4 key购买 nike

我有下面定义的一对多关系

public class Student{
[key]
public string Id{get; set;}
public string name{get; set:}
public virtual List<Enrollment> Enrol{get; set;}
}

public class Enrollment{
public int Id{get; set;}

[ForeignKey("student")]
public String StudentId{get; set;}
Public string ISC{get; set;}
public virtual Student student{get; set;}

}

我有一个表单允许我编辑学生实体,包括添加或删除一些注册。提交表格后保存此数据时,注册是否也会更新,还是我必须单独更新注册?理想情况下,应该删除注册然后重新插入还是只更新(并且仅在新项目多于现有项目时才插入)?

最佳答案

您需要遍历图表。 EF 不会自动更新关系。

这个响应有一个很好的插入/更新删除子对象的例子: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

还有一个很好的扩展方法,可以添加图形遍历功能: https://github.com/refactorthis/GraphDiff

关于c# - MVC : Updating One to Many Relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735219/

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