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

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

问题描述

让我很快地描述了我的问题。

Let me quickly describe my problem.

我的 5个数据库 5的客户和每个人都有所谓SubnetSettings相同的表

我已经创建了一个DropDownList选择客户和意志展示了属于选定客户,并让我创建,编辑和删除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 <保存方法/ p>

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.

推荐答案

合成答案客观:
你要更新的对象不从基来,这是错误的原因。对象来自于观的职务。

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.

解决方案是检索的基础对象,这将使实体框架知道并在上下文管理的对象。该你必须得到已经从景观变化,包括由实体控制的对象的每个值

The solution is to retrieve the object from base, this will make Entity Framework know and manage the object in the context. The you 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天全站免登陆