【问题标题】:one of my entities isn't saving in entity framework when I update through my repository当我通过我的存储库更新时,我的一个实体没有保存在实体框架中
【发布时间】:2015-04-14 01:31:06
【问题描述】:

我有两个实体“Student”和“StudentImage”。我正在使用 MVC 5,我有一个表格,学生用图像填写他/她的个人资料,然后将其保存到 student 和 studentimage 表中。当没有以前的学生时,我可以同时插入学生和学生图片,但是当我的存储库进行更新时,只有学生会更新,学生图片不会更新。 *我已经完成了一个 sql profiler 检查,它看起来不像 studentimage 被用于任何更新查询。另一件事是它没有抛出任何错误。它完成并返回到我的视图。

这是我的实体

public class Student
{
    public int StudentId { get; set; }

    [Index(IsUnique=true)]
    [Required]
    [MaxLength(128)]
    public string AspNetUserRefId { get; set; }

    public virtual StudentImage StudentImage { get; set; } //one-to-one

    // other members below
}

public class StudentImage
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

    public byte[] Image { get; set; }

    public virtual Student Student { get; set; } // one-to-one
}

这是我在存储库中为我的学生提供的更新方法

public void InsertOrUpdate(Student student)
{
    if (!context.Students.Any(u => u.AspNetUserRefId == student.AspNetUserRefId))
    {
        // New entity
        context.Students.Add(student);
    }
    else
    {
        // Existing entity
        context.Entry(student).State = EntityState.Modified;
    }
}

这是我发布的用于保存的控制器方法编辑

[HttpPost]
    public ActionResult Edit(EditStudentViewModel studentViewModel)
    {
        if (ModelState.IsValid)
        {
            byte[] array;
            using (MemoryStream ms = new MemoryStream())
            {
                studentViewModel.StudentImageFileBase.InputStream.CopyTo(ms);
                array = ms.GetBuffer();
            } 

            var student = new Student
            {
                AspNetUserRefId = studentViewModel.AspNetUserRefId,
                CatchPhrase = studentViewModel.CatchPhrase,
                StartedPracticing = Convert.ToInt16(studentViewModel.SelectedYearId),
                Location = studentViewModel.Location,
                Education = studentViewModel.Education,
                Work = studentViewModel.Work,
                SelfDescription = studentViewModel.SelfDescription,
                Inspirations = studentViewModel.Inspirations,
                StudentId = studentViewModel.StudentId,
                StudentImage = new StudentImage { Image = array, StudentId = studentViewModel.StudentId}
            };

            studentRepository.InsertOrUpdate(student);
            studentRepository.Save();
            return RedirectToAction("Edit", "Student");
        } else {
            return View();
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc entity-framework asp.net-mvc-5


    【解决方案1】:

    我认为您还需要将附加图像标记为已更改...

    // Existing entity
    context.Entry(student).State = EntityState.Modified;
    context.Entry(student.StudentImage).State = EntityState.Modified;
    

    ...因为我认为它不会自动将修改后的状态传播到附加的学生图像记录。假设 .StudentImage 实例已经是上下文的一部分。如果没有,则需要添加它。

    【讨论】:

    • 那么如果我第一次插入一行,为什么它会传播到 studentimage?不是更新而是插入!
    • 因为当您添加一条记录时,它会自动为整个连接记录树执行添加。但是当改变单个记录的状态时,它只会改变那个记录,它不会自动传播。这就是实体框架的实现方式。
    • 好的明白了!但添加 context.Entry(student.StudentImage).State = EntityState.Modified;到我在存储库中的 InsertUPdate 似乎有点不正确!?似乎有一种更好的方法我不知道,因为我是 EF 的新手。
    【解决方案2】:

    您是否尝试将实体附加到上下文,然后将条目设置为已修改?:

    context.Set<Students>().Attach(student);
    var entry = context.Entry(student);
    entry.State = EntityState.Modified;
    context.SaveChanges();
    

    【讨论】:

      【解决方案3】:

      使用实体框架编辑数据时,需要加载原始对象以便应用更改。您正在使用context.Entry(student) 进行此操作;但是,您能否确认相关对象StudentImage 也正在加载?

      我注意到您将相关对象定义为public virtual StudentImage StudentImage { get; set; }。 EF 默认为 virtual 属性使用延迟加载。去掉virtual关键字会发生什么?

      假设它仍然处于活动状态,此页面包含有关 Loading Related Entities 的更多信息。

      【讨论】:

      • 哪一部分?你的意思是虚拟声明?还是整条线?记住我想在这里建立一对一的关系
      • 尝试只删除 virtual 关键字。
      • 你好 TLS。我删除了虚拟并更新了数据库,但是当我在我的存储库中运行 Find(string id) 以获取学生时,它返回 StudentImage 为 null。和以前一样,它返回 StudentImage 和 Student,无论它是否延迟加载,我都会猜测默认情况下会说懒惰。所以,除非我在这里缺少其他东西。我将在下面使用 Phile Wright 的解决方案。我检查了其他相关帖子,这似乎是解决方案。
      • 合适。如果它解决了您的问题,请务必接受他的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多