2013-05-14 66 views
0

我正在使用MVC应用程序。context.SaveChanges()不持久化数据库中的数据

当我调用context.SaveChanges来更新特定的记录。更新未在数据库中注册。我也没有得到任何运行时错误。所有通知是我的记录没有更新。我仍然看到相同的值。插入功能完美地工作。

enter code here 

public Admission Update(int stuid){ 

     VDData.VidyaDaanEntities context = new VDData.VidyaDaanEntities(); 
     VDData.Student_Master studentmaster = new VDData.Student_Master(); 

     studentmaster.Student_ID = stuid; 
     studentmaster.Student_First_Name = this.FirstName; 
     studentmaster.Student_Middle_Name = this.MiddleName; 
     studentmaster.Student_Last_Name = this.LastName; 
     studentmaster.Student_Address_1 = this.Address; 
     studentmaster.Student_Address_2 = this.Address2; 
     studentmaster.Student_City = this.City; 
     studentmaster.Student_State = this.State; 
     studentmaster.Student_Pin_Code = this.Pincode; 

     context.SaveChanges(); // here it wont give any kind of error. it runs sucessfully. } 
+1

你可以提供一些代码! – Akrem 2013-05-14 11:36:59

+0

在保存更改之前是否更新模型? – Prashant16 2013-05-14 11:40:10

+0

您是否附加了该实体?你可以在这里找到有用的东西http://msdn.microsoft.com/en-us/data/jj592676.aspx – vmeln 2013-05-14 13:05:24

回答

0

好像要更新,更换context.SaveChanges();所以你的代码应该是

VDData.Student_Master studentmaster = context.Student_Masters.Single(p=>p.Student_ID == stuid); 

,你不应该改变student_id数据如果是首要的关键。

public Admission Update(int stuid){ 

     VDData.VidyaDaanEntities context = new VDData.VidyaDaanEntities(); 
     //VDData.Student_Master studentmaster = new VDData.Student_Master(); 
//REPLACE WITH 
VDData.Student_Master studentmaster = context.Student_Masters.Where(p=>p.Student_ID == stuid); 

     studentmaster.Student_ID = stuid; 
     studentmaster.Student_First_Name = this.FirstName; 
     studentmaster.Student_Middle_Name = this.MiddleName; 
     studentmaster.Student_Last_Name = this.LastName; 
     studentmaster.Student_Address_1 = this.Address; 
     studentmaster.Student_Address_2 = this.Address2; 
     studentmaster.Student_City = this.City; 
     studentmaster.Student_State = this.State; 
     studentmaster.Student_Pin_Code = this.Pincode; 

     context.SaveChanges(); 
+0

VDData.Student_Master studentmaster = context.Student_Masters.Where(p => p.Student_ID == stuid);它给出错误错误\t无法隐式地将类型'System.Linq.IQueryable '转换为'VDData.Student_Master'。存在明确的转换(是否缺少转换?)VDModel \ Models \ Admission \ Admission.cs – Harshal 2013-05-15 06:15:43

+0

将Where()更改为Single()并且它将工作。 – cherhan 2013-05-15 06:21:02

1

首先得到你所要更新的实体:

var entity = obj.GetEntity(id); 
entity.col1 = "value"; 
context.SaveChanges(entity); 

希望这将有助于。

+0

谢谢回复我。我正在尝试你的代码,但它不会工作。 – Harshal 2013-05-15 05:17:13

0

之前

context.SaveChanges(); 

你需要调用这个

context.Student_Masters.Add(studentmaster); 

编辑:介绍AbstractionContext class和你的上下文类象下面创建一个方法,那么你可以调用它,每当你想创建或更新你的对象。

public void SaveStudent_Master(Student_Master studentmaster) 
    { 
     using (var context = new VDData.VidyaDaanEntities()) 
     { 
      if (studentmaster.Student_ID == 0) 
      { 
       context.Student_Masters.Add(studentmaster); 
      } 
      else if (studentmaster.Student_ID > 0) 
      { 
       //This Updates N-Level deep Object grapgh 
       //This is important for Updates 
       var currentStudent_Master = context.Student_Masters 
        .Single(s => s.Student_ID == studentmaster.Student_ID); 

       context.Entry(currentStudent_Master).CurrentValues.SetValues(studentmaster);     
      } 
      context.SaveChanges(); 
     } 

然后在你的控制器与_context.SaveStudent_Master(studentmaster);

+0

其实我想更新记录和建议的代码插入记录到表中。 – Harshal 2013-05-15 05:34:31

+0

@Harshal是的,我看到现在,你没有显示足够的代码 – Komengem 2013-05-15 05:36:16