gpt4 book ai didi

c# - 为什么我的 ASP.NET MVC 4 应用程序创建新实体而不是更新旧实体?

转载 作者:行者123 更新时间:2023-12-02 20:46:49 25 4
gpt4 key购买 nike

编辑:我选择的解决方案可能不是最好的,但它确实有效。我将在下周检查我的代码(一旦这个项目完成),当我明白出了什么问题时,我将更新我的问题。

我将 ASP.NET MVC 4 框架与 Entity 5 结合使用。以下是一些代码:

要实例化并保存(新鲜)在数据库中的类:

    public class ClassCancellation
{
[Key]
public int Id { get; set; }
public Faculty Professor { get; set; }
public DateTime CancelledOn { get; set; }
public Course Course { get; set; }
[Required]
public ClassDate ClassCancelled { get; set; }
public Message CancellationMessage { get; set; }
[Required]
public List<Student> Students { get; set; }
}

它是从名为 CancellationFull 的 View 模型映射的(使用 AutoMapper):

public class CancellationForList
{
[Key]
public int Id { get; set; }
public CourseForList Course { get; set; }
public ClassDateForList ClassCancelled { get; set; }
}

public class CancellationFull : CancellationForList
{
public CancellationFull()
{
this.Students = new List<StudentForList>();
}
public FacultyForList Professor { get; set; }
public MessageForList CancellationMessage { get; set; }
public DateTime CancelledOn { get; set; }
public List<StudentForList> Students { get; set; }
}

这是将 CancellationFull 变成的 repo 方法进入ClassCancellation然后将其保存到数据库:

    public CancellationFull createClassCancellation(CancellationFull c) 
{
ClassCancellation newCancellation = Mapper.Map<ClassCancellation>(c);

dc.ClassCancellations.Add(newCancellation);
dc.SaveChanges();

return Mapper.Map<CancellationFull>(dc.ClassCancellations.FirstOrDefault(cc => cc.Id == newCancellation.Id));
}

为什么,天哪,数据库为什么要为 Faculty 创建新对象?和CourseId是否提供了每个现有实体对应项的(主键)?它也可能对 Student 做同样的事情。对象,但我没有仔细观察过。

ClassCancellation之前实例保存到数据库中,调试器显示它的属性 Professor类型 FacultyCourse类型 Course具有正确的主键 - 也就是说,我试图引用新的 ClassCancellation 来更新这些类型的已存在实体的主键对象。

快把我逼疯了。请随时要求澄清!

编辑:

这是 CancellationFull 的逻辑viewmodel 是根据表单数据和有关从各自存储库检索的现有对象的 viewmodel 构建的:

     newCancellation = new CancellationFull();
newCancellation.CancelledOn = DateTime.Now;
newCancellation.ClassCancelled = repoClass.getClassDateForListById(Int32.Parse(classIds[i]));
newCancellation.Course = repoCourse.getForList(newCancellation.ClassCancelled.Course.Id);
newCancellation.CancellationMessage = repoMessage.getMessageForList(newMessage.Id);
newCancellation.Professor = repoFac.getFacultyForList((int)Session["facId"]);

var students = repoStudent.getStudentsForListByCourse(newCancellation.Course.Id);

foreach ( var student in students )
{
newCancellation.Students.Add(student);
}

repoCancellation.createClassCancellation(newCancellation);

以下是其中一种存储库方法的示例(其余方法非常相似):

    public CourseForList getForList(int? id)
{
return Mapper.Map<CourseForList>(dc.Courses.FirstOrDefault(c => c.Id == id));
}

最佳答案

我发现最简单的解决方案是在更新模型时,清除所有相关实体,然后重新添加它们。

即:

 newCancellation.Students.Clear();

foreach ( var student in students )
{
newCancellation.Students.Add(student);
}

关于c# - 为什么我的 ASP.NET MVC 4 应用程序创建新实体而不是更新旧实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144941/

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