当我通过我的存储库更新时,我的一个实体没有保存在 entity framework 中

one of my entities isn't saving in entity framework when I update through my repository

我有两个实体 'Student' 和 'StudentImage'。我使用的是 MVC 5,我有一个表格,学生可以在其中填写 his/her 配置文件和图像,然后将其保存到学生和 studentimage table。当没有以前的学生时,我可以同时插入学生和学生图像,但是当我的存储库进行更新时,只有学生得到更新而学生图像没有得到更新。 *我已经完成了 sql 探查器检查,它看起来不像是在任何更新查询中使用了 studentimage。另一件事是它没有抛出任何错误。它完成了,returns 在我看来。

这是我的实体

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();
        }
    }

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

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

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

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

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

使用 Entity Framework 编辑数据时,需要加载原始对象以便应用更改。你正在用 context.Entry(student);但是,您能否确认相关对象 StudentImage 也正在加载?

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

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