Error While Udpate Data in entity Framework of MVC
//This Is Get Action Of Edit
{
if (!id.HasValue)
return RedirectToAction("index");
using (FirstAngularJsWithMVCEntities db = new FirstAngularJsWithMVCEntities())
{
var Employee = db.Employees.Find(id);
return View(Employee);
}
}
//This Is Post Action Of Edit
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Employee employee)
{
if (!ModelState.IsValid)
return View(employee);
using (FirstAngularJsWithMVCEntities db = new FirstAngularJsWithMVCEntities())
{
var Emp=db.Employees.Find(employee.id);
if (Emp != null)
{
//Emp.Fname = employee.Fname;
//Emp.address = employee.address;
//Emp. Age = employee.Age;
//Emp. phoneno = employee.phoneno;
//Emp. UserId = employee.UserId;
db.Entry(employee).State = EntityState.Modified;
if (db.SaveChanges() > 0)
{
return View("index");
}
else
{
return View(employee);
}
}
else
return View(employee);
}
}
//Problem:-
(Now The Problem Is Your entity is not modified so you need to Follow The Solution
System.InvalidOperationException
HResult=0x80131509
Message=Attaching an entity of type 'AngularFirstWithMVC.DataEntity.Employee' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation)
at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<Attach>b__9()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)
at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)
at AngularFirstWithMVC.Controllers.EmployeeController.Edit(Employee employee) in D:\Angular demos\AngularFirstWithMVC\AngularFirstWithMVC\Controllers\EmployeeController.cs:line 111
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__11_0()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
Soulution:-
Finally The Solution Is Update Code in Post Method of Edit action
like this
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Employee employee)
{
if (!ModelState.IsValid)
return View(employee);
using (FirstAngularJsWithMVCEntities db = new FirstAngularJsWithMVCEntities())
{
var Emp=db.Employees.Find(employee.id);
if (Emp != null)
{
db.Entry(Emp).CurrentValues.SetValues(employee);
if (db.SaveChanges() > 0)
{
return View("index");
}
else
{
return View(employee);
}
}
else
return View(employee);
}
}
No comments:
Post a Comment