Let me quickly describe my problem.
I have 5 databases for 5 customers and each has the same table called SubnetSettings.
I already created a dropdownlist to select a customer and will shows up the SubnetSetting table which belong to selected customer and allow me to create, edit and delete.
I can create, delete without problem but when I want to edit the data it brings the error:
Server Error in '/TMS' Application.
Attaching an entity of type 'CFS.Domain.Entities.SubnetSettings' 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 my Edit in my Controller
// GET: /SubnetSettings/Edit1/5
public ActionResult Edit1(short? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SubnetSettings subnetsettings = detailView.SubnetSettings.SingleOrDefault(t => t.Id == id);
if (subnetsettings == null)
{
return HttpNotFound();
}
return View(subnetsettings);
}
// POST: /SubnetSettings/Edit1/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit1([Bind(Include = "Id,Name,fDialUp,fPulse,fUseExternalGSMModem,fGsmDialUp,bUploadMethodId")] SubnetSettings subnetsettings)
{
if (ModelState.IsValid)
{
templateDb2.Save(subnetsettings);
return RedirectToAction("Index");
}
return View(subnetsettings);
}
Here is the Save method in the EF
public SubnetSettings Save(SubnetSettings subnetsettings) {
if (subnetsettings.Id == 0){
context.SubnetSettings.Add(subnetsettings);
}
else {
context.SubnetSettings.Attach(subnetsettings);
context.Entry(subnetsettings).State = EntityState.Modified;
}
context.SaveChanges();
return subnetsettings;
}
I know it's hard to understand other people's code. So any recommend or suggestion is very thankful.