neel k

neel k

  • NA
  • 143
  • 136.3k

MVC 5 error

Mar 18 2015 10:38 AM
Hi 
 
I am using MVC 5 , In that when I click the save button i got an error.
 
Attaching an entity of type '' 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 
 
Here is controller code:
 
 
[HttpPost]
public ActionResult ApplicationEdit(external_applications application_new, string ApplicationWorlds)
{
var appWorld = (from w in db.sp_get_appworlds()
select w).ToList();
ViewBag.ApplicationWorlds = new SelectList(appWorld, "id", "name", ApplicationWorlds);
external_applications dbAppEdit_old = GetApplicationDetailsByAppId(application_new.id); // old data
DomainContext CurrentLoginUser = ActiveDirectory_AccessModel.GetUserDetails(); // Username
ArrayList list = new ArrayList();
if (dbAppEdit_old.description != application_new.description)
{
string description = "description" + "---" + dbAppEdit_old.description + "---" + application_new.description;
list.Add(description);
}
if (dbAppEdit_old.registrationURL_development != application_new.registrationURL_development)
{
string registrationURL_dev = "registrationURL_development" + "---" + dbAppEdit_old.registrationURL_development + "---" + application_new.registrationURL_development;
list.Add(registrationURL_dev);
}
if (dbAppEdit_old.registrationURL_production != application_new.registrationURL_production)
{
string registrationURL_prod = "registrationURL_production" + "---" + dbAppEdit_old.registrationURL_production + "---" + application_new.registrationURL_production;
list.Add(registrationURL_prod);
}
if (dbAppEdit_old.loginURL_production != application_new.loginURL_production)
{
string loginURL_prod = "loginURL_production" + "---" + dbAppEdit_old.loginURL_production + "---" + application_new.loginURL_production;
list.Add(loginURL_prod);
}
if (dbAppEdit_old.loginURL_development != application_new.loginURL_development)
{
string loginURL_dev = "loginURL_development" + "---" + dbAppEdit_old.loginURL_development + "---" + application_new.loginURL_development;
list.Add(loginURL_dev);
}
if (dbAppEdit_old.update_service_URL != application_new.update_service_URL)
{
string updateserviceURL = "update_service_URL" + "---" + dbAppEdit_old.update_service_URL + "---" + application_new.update_service_URL;
list.Add(updateserviceURL);
}
if (dbAppEdit_old.notify_service_URL != application_new.notify_service_URL)
{
string notifyserviceURL = "notify_service_URL" + "---" + dbAppEdit_old.notify_service_URL + "---" + application_new.notify_service_URL;
list.Add(notifyserviceURL);
}
if (dbAppEdit_old.maintenance_start != application_new.maintenance_start)
{
string maintenancestart = "maintenance_start" + "---" + dbAppEdit_old.maintenance_start + "---" + application_new.maintenance_start;
list.Add(maintenancestart);
}
if (dbAppEdit_old.maintenance_end != application_new.maintenance_end)
{
string maintenanceend = "maintenance_end" + "---" + dbAppEdit_old.maintenance_end + "---" + application_new.maintenance_end;
list.Add(maintenanceend);
}
if (dbAppEdit_old.status != application_new.status)
{
string userStatus = "status" + "---" + dbAppEdit_old.status + "---" + application_new.status;
list.Add(userStatus);
}
if (dbAppEdit_old.prevent_display != application_new.prevent_display)
{
string preventdisplay = "prevent_display" + "---" + dbAppEdit_old.prevent_display + "---" + application_new.prevent_display;
list.Add(preventdisplay);
}
if (dbAppEdit_old.icon != application_new.icon)
{
string icon = "icon" + "---" + dbAppEdit_old.icon + "---" + application_new.icon;
list.Add(icon);
}
if (dbAppEdit_old.world_id != application_new.world_id)
{
string worldid = "world_id" + "---" + dbAppEdit_old.world_id + "---" + application_new.world_id;
list.Add(worldid);
}
if (dbAppEdit_old.ordernumber != application_new.ordernumber)
{
string ordernum = "ordernumber" + "---" + dbAppEdit_old.ordernumber + "---" + application_new.ordernumber;
list.Add(ordernum);
}
if (dbAppEdit_old.public_app != application_new.public_app)
{
string publicapp = "public_app" + "---" + dbAppEdit_old.public_app + "---" + application_new.public_app;
list.Add(publicapp);
}
if (dbAppEdit_old.IntranetOnly != application_new.IntranetOnly)
{
string IntranetOnly = "IntranetOnly" + "---" + dbAppEdit_old.IntranetOnly + "---" + application_new.IntranetOnly;
list.Add(IntranetOnly);
}
if (dbAppEdit_old.newsystem_live != application_new.newsystem_live)
{
string newsystemlive = "newsystem_live" + "---" + dbAppEdit_old.newsystem_live + "---" + application_new.newsystem_live;
list.Add(newsystemlive);
}
if(list.Count > 0)
{
//db.Entry(application_new).State = System.Data.Entity.EntityState.Modified;
db.external_applications.Attach(application_new);
//db.external_applications.Find(application_new.description);
db.SaveChanges();
}
 
Thanks 

Answers (1)