Abhilash J A

Abhilash J A

  • 502
  • 2.4k
  • 597.1k

EntityState.Modified not working properly

Feb 16 2017 1:36 AM
Hello everyone,
 
I am MVC5, I have Parter table in the SQL Server DB. And I am using Entitty Framework to Save and Update the table. I am saving the data two times from different views. But null updating 1st saved data after saving second data.
 
  1. public static int Partner_AddUpdate(Partner t_Entity)  
  2.         {  
  3.             int retValue = -1;  
  4.             using (var t_Context = new ReboxEntities())  
  5.             {  
  6.                 using (var transaction = t_Context.Database.BeginTransaction())  
  7.                 {  
  8.                     try  
  9.                     {  
  10.                         if (t_Entity.idPartner > 0)  
  11.                         {  
  12.                             t_Context.Entry(t_Entity).State = System.Data.Entity.EntityState.Modified;  
  13.                         }  
  14.                         else  
  15.                         {  
  16.                             t_Context.Entry(t_Entity).State = System.Data.Entity.EntityState.Added;  
  17.                         }  
  18.   
  19.                         t_Context.SaveChanges();  
  20.   
  21.                         retValue = t_Entity.idPartner;  
  22.   
  23.                         transaction.Commit();  
  24.                     }  
  25.                     catch (Exception exc)  
  26.                     {  
  27.                         transaction.Rollback();  
  28.                     }  
  29.                 }  
  30.             }  
  31.             return retValue;  
  32.         } 
 Please help me...

Answers (2)