Abhilash J A

Abhilash J A

  • NA
  • 2.4k
  • 584.2k

Error attaching entity of id when trying to update MVC5

Feb 20 2017 4:12 AM
Hi,
 
I am getting error whed trying to update table.
 
  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.                             Partner t_EntityModified = t_Context.Partners.Single(m => m.idPartner == t_Entity.idPartner);  
  13.                             
  14.                             t_Entity.LoginName = t_EntityModified.LoginName;  
  15.                             t_Entity.Password = t_EntityModified.Password;  
  16.                             t_Entity.PartnerTypeName = t_EntityModified.PartnerTypeName;  
  17.                             t_Entity.IncorporationType = t_EntityModified.IncorporationType;                             
  18.                               
  19.                             t_Context.Entry(t_Entity).State = System.Data.Entity.EntityState.Modified;  
  20.                         }  
  21.                         else  
  22.                         {  
  23.                             t_Context.Entry(t_Entity).State = System.Data.Entity.EntityState.Added;  
  24.                         }  
  25.   
  26.                         t_Context.SaveChanges();  
  27.   
  28.                         retValue = t_Entity.idPartner;  
  29.   
  30.                         transaction.Commit();  
  31.                     }  
  32.                     catch (Exception exc)  
  33.                     {  
  34.                         transaction.Rollback();  
  35.                     }  
  36.                 }  
  37.             }  
  38.             return retValue;  
  39.         } 
 

Answers (2)