简体   繁体   English

实体框架更新实体错误

[英]Entity Framework Updating Entities error

I am getting this a generic error:- 我得到这个一般性错误:-

Additional information: Attaching an entity of type 'entity' 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 在这种情况下,请使用“添加”方法或“已添加”实体状态来跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”

Its happening below on the line when the entity is trying to change its state. 当实体尝试更改其状态时,它会在下面发生。

public void InsertOrUpdate(T entity)
    {
        if (entity.Id == default(int))
        {
            entity.Created = DateTime.Now;
            entity.LastEdit = DateTime.Now;

            Context.Initial.Add(initial);
        }
        else
        {
            entity.LastEdit = DateTime.Now;
            Context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
        }
    }

However, I am getting it when I 但是,当我得到它时

  • Reload the context ie initializing the repository from the view. 重新加载上下文,即从视图初始化存储库。
  • Using the attach properly instead Context.T.Attach(entity) 正确使用附加代替Context.T.Attach(entity)

And when I dont change the state info or use attach, it doesnt update anything. 当我不更改状态信息或使用附加信息时,它不会更新任何内容。

Here is my Controller code 这是我的控制器代码

[HttpPost]
[Authorize]
public ActionResult Create(NewViewModel viewModel)
{
   if (ModelState.IsValid)
   {
      m_CTS.InsertOrUpdate(viewModel.entity);
      m_CTS.Save();

      // My big problem is that the Primary key that 
      // was generated doesnt come back from the view 
      // once edited, so I have created a property in 
      // the viewmodel to handle this

      viewModel.ID = viewModel.entity.Id;

      return RedirectToAction("Edit", new { id = viewModel.entity.Id });
    }
    else
    {
      return View("New", viewModel);
    }
}

[HttpGet]
[Authorize]
public ViewResult Edit(int id)
{
  // Getting this from the same context of when it was created
  viewModel.entity = respoitory.Find(id);

  return View(viewModel);
}

[HttpPost]
[Authorize]
public ActionResult Edit(NewViewModel viewModel)
{
   // Could be the problem : As I said above the ID on the entity.id 
   // never comes back from the view (because I am using a viewmodel?) 
   // So I need to assign the ViewModel ID to the entity model id 
   // before I try and update it

   viewModel.entity.Id = viewModel.ID;

   m_CTS.InsertOrUpdate(viewModel.entity);
   m_CTS.Save();

   return RedirectToAction("Edit", new { id = viewModel.entity.Id });
}

Is there a reason why I am getting this error with the above code? 使用以上代码会收到此错误吗?

Thanks 谢谢

You have identified the problem within the comments of your code: 您已在代码注释中找到问题:

       // My big problem is that the Primary key that 
       // was generated doesnt come back from the view 
       // once edited, so I have created a property in the viewmodel to handle this

To fix this, add @Html.HiddenFor(m => m.Id) to your view. 要解决此问题,请在您的视图中添加@Html.HiddenFor(m => m.Id) Now when you POST to the controller, your view model parameter will contain the Id field that you sent to the view in the first place. 现在,当您发布到控制器时,您的视图模型参数将包含您首先发送到视图的Id字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM