附加类型的实体失败,因为相同类型的另一个实体已具有相同的主键值. [英] Attaching an entity of type failed because another entity of the same type already has the same primary key value.

查看:29
本文介绍了附加类型的实体失败,因为相同类型的另一个实体已具有相同的主键值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我快速描述一下我的问题.

Let me quickly describe my problem.

我有 5 个数据库供 5 个客户使用,并且每个客户都有相同的表,称为 SubnetSettings.

I have 5 databases for 5 customers and each has the same table called SubnetSettings.

我已经创建了一个下拉列表来选择一个客户,并将显示属于所选客户的 SubnetSetting 表,并允许我创建、编辑和删除.

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:

/TMS"应用程序中的服务器错误.

附加类型为CFS.Domain.Entities.SubnetSettings"的实体失败,因为相同类型的另一个实体已经具有相同的主键值.如果图中的任何实体具有冲突的键值,则在使用附加"方法或将实体的状态设置为未更改"或已修改"时,可能会发生这种情况.这可能是因为某些实体是新的,尚未收到数据库生成的键值.在这种情况下,使用添加"方法或添加"实体状态来跟踪图形,然后根据需要将非新实体的状态设置为未更改"或已修改".

这是我在控制器中的编辑

    // 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);
    }

这里是EF中的Save方法

     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.

推荐答案

客观综合答案:您尝试更新的对象不是来自基础,这是错误的原因.对象来自视图的帖子.

Synthesizing the answer objectively: The object you are trying to update don't came from the base, this is the reason of the error. The object came from the post of the View.

解决方案是从 base 中检索对象,这将使 Entity Framework 知道并管理上下文中的对象.然后,您将必须获取从 View 更改的每个值,并包含在由实体控制的对象中.

The solution is to retrieve the object from base, this will make Entity Framework know and manage the object in the context. Then you will have to get each value that has changed from View and consist in the object controlled by the Entity.

// POST: /SubnetSettings/Edit1/5   
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit1([Bind(Include = "Id,Name,fDialUp,fPulse,fUseExternalGSMModem,fGsmDialUp,bUploadMethodId")] SubnetSettings subnetsettings)
{
    if (ModelState.IsValid)
        {
            //Retrieve from base by id
            SubnetSettings objFromBase = templateDb2.GetById(subnetsettings.Id);

            //This will put all attributes of subnetsettings in objFromBase
            FunctionConsist(objFromBase, subnetsettings)

            templateDb2.Save(objFromBase);   
            //templateDb2.Save(subnetsettings);   

            return RedirectToAction("Index");
        }
    return View(subnetsettings);
}

这篇关于附加类型的实体失败,因为相同类型的另一个实体已具有相同的主键值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆