当Object从另一个类返回时,无法在Entity Framework中保存更改

时间:2017-05-02 14:37:34

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

我正在开发简单的MVC应用程序,用于更新数据库中的记录。我已经使用BAL和Controller来获取记录和更新,如下所示

Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.DAL
{
    public class Student
    {
        DemoDatabaseEntities db = new DemoDatabaseEntities();
        public Student_Mast getStudentByID()
        {
            Student_Mast student = db.Student_Mast.Where(i => i.StudID == 1).FirstOrDefault();
            return student;
        }
    }
}

StudentController.cs

public class UpdateController : Controller
{
    DemoDatabaseEntities db = new DemoDatabaseEntities();
    Student _bal = new Student();
    / GET: Update
    public ActionResult Index()
    {
        Student_Mast model = new Student_Mast();
        model = _bal.getStudentByID();
        model.First_Name = "Hardik";
        model.Last_Name = "Gondalia";
        db.SaveChanges();
        return View();
    }
}

但是db.savechanges()没有更新记录 如果我更新下面的记录,它将起作用而不是这个 StudentController.cs

public class UpdateController : Controller
{
    DemoDatabaseEntities db = new DemoDatabaseEntities();
    Student _bal = new Student();
    / GET: Update
    public ActionResult Index()
    {
        Student_Mast model = new Student_Mast();
        model = db.Student_Mast.Where(i => i.StudID == 1).FirstOrDefault();
        model.First_Name = "Hardik";
        model.Last_Name = "Gondalia";
        db.SaveChanges();
        return View();
    }
}

4 个答案:

答案 0 :(得分:1)

由于您从DbContext的其他实例检索记录,因此无法保存该记录。 解决方案是在Student类中添加一个构造函数,该构造函数接受一个DbContext实例并将其保存到您拥有的变量中。

public class Student
{
    private DemoDatabaseEntities db = null;

    public Student(DemoDatabaseEntities dbContext){
    {
        this.db = dbContext;
    }

    public Student_Mast getStudentByID()
    {
        Student_Mast student = db.Student_Mast.Where(i => i.StudID == 1).FirstOrDefault();
        return student;
    }
}

答案 1 :(得分:1)

将相同的数据库实体类DemoDatabaseEntities传递给Student类。这样做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.DAL
{
    public class Student
    {
        DemoDatabaseEntities db;
        public Student(DemoDatabaseEntities _db)
        {
           this.db = _db
        }

        public Student_Mast getStudentByID()
        {
            Student_Mast student = db.Student_Mast
                             .Where(i => i.StudID == 1).FirstOrDefault();
            return student;
        }
    }
}

现在在StudentController你会做到这一点

public class UpdateController : Controller
{
    DemoDatabaseEntities db = new DemoDatabaseEntities();
    Student _bal = new Student(db);
    / GET: Update
    public ActionResult Index()
    {
        Student_Mast model = new Student_Mast();
        model = _bal.getStudentByID();
        model.First_Name = "Hardik";
        model.Last_Name = "Gondalia";
        db.SaveChanges();
        return View();
    }
}

这实际上是为数据库操作使用相同的数据库上下文对象,即让学生和更新回db。

答案 2 :(得分:1)

这种情况正在发生,因为_bal.getStudentByID();正在从另一个EF上下文获取Student实体,并且此实体与db上下文分离。

您可以将实体附加到您的上下文,或将现有上下文作为依赖项传递给Student

在第一种情况下, StudentController.cs 的更新版本将包含以下代码:

public class UpdateController : Controller
{
    DemoDatabaseEntities db = new DemoDatabaseEntities();
    Student _bal = new Student();
    / GET: Update
    public ActionResult Index()
    {
        Student_Mast model = new Student_Mast();
        model = _bal.getStudentByID();
        db.Entry(model).State = EntityState.Modified

        model.First_Name = "Hardik";
        model.Last_Name = "Gondalia";
        db.SaveChanges();
        return View();
    }
}

上面已经回答了第二种情况发生的事情

答案 3 :(得分:1)

您的第一个示例中的问题是因为您从不同的数据上下文实例获取student实体。这里有两个选项,在Update类中实现Student方法,以便在学生实体所在的同一DbContext实例中保存更改,这是一个糟糕的设计,你不应该'在您的实体中定义这些方法(研究RepositoryUnitOfWork模式),或者在您尝试第二个示例时在控制器中定义更新:

 var model = db.Student_Mast.FirstOrDefault(i => i.StudID == 1);
 model.First_Name = "Hardik";
 model.Last_Name = "Gondalia";
 db.SaveChanges();