.net c#与多对多关系更新实体

Updating entity with many to many relationship
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我有一个与多对多关系的模型:

public class Transport
{
    ...
    public virtual ICollection<Remark> CargoRemarks { get; set; }
    ...
}

public class Remark
{
    public virtual ICollection<Transport> Transports { get; set; }
}

在某些情况下,我必须更新包含某些说明的运输模型.添加或删除备注时,未附加模型(由于某些体系结构决策,无法完成此操作).

但是,如果未更改传输中的任何备注,传输对象的更新将失败:

'...Transport' 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.

例:

  • 我的传输模型已创建,并带有一些注释插入到数据库中.一切工作正常.
  • 稍后,将再次加载并分离此插入的模型.
  • 在不更改(未添加或删除模型中的任何注释)的情况下,我想更新模型.这导致此错误信息.

这是我用来更新实体的方法:

public virtual void Update(TEntity entityToUpdate)
{
    dbSet.Attach(entityToUpdate);
    context.Entry(entityToUpdate).State = EntityState.Modified;
}
速聊1:
我现在有同样的问题.
解决过程1

我认为您的问题与M2M关系无关.这在Attach中很常见.

我建议的一件事是考虑从db而不是Attach重新加载实体.其背后的原因是,您不能确定该实体是否以尝试附加的形式存在于数据库中.

如果这不是一种选择,您是否尝试过不附加实体?如果已经设置了实体密钥,则应根据以下内容正确更新自身:

https://msdn.microsoft.com/zh-CN/data/jj592676.aspx

速聊1:
该问题甚至出现在不附加实体的情况下.当我不更改(添加/删除)任何评论时,不会出现此问题.

I have a model with a many to many relationship:

public class Transport
{
    ...
    public virtual ICollection<Remark> CargoRemarks { get; set; }
    ...
}

public class Remark
{
    public virtual ICollection<Transport> Transports { get; set; }
}

In some situations I have to update my Transport model containing some remarks. When adding or removing Remarks the models are not attached (this can't be done due to some architectural decisions).

However without having changed any Remarks in my Transport, the update of my transport object fails:

'...Transport' 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.

Example:

  • My transport model is created and inserted into the database with some remarks. Everything is working fine and correctly.
  • At a later time this inserted model is loaded again and detached.
  • Without having changed (not added nor removed any remark in the model) my model I want to update it. This leads to this error message.

This is the method I'm calling for updating an entity:

public virtual void Update(TEntity entityToUpdate)
{
    dbSet.Attach(entityToUpdate);
    context.Entry(entityToUpdate).State = EntityState.Modified;
}
Talk1:
Im having the same problem now.
Solutions1

From my perspective your issue is not related to the M2M relationship. It's common with Attach.

One thing that I would recommend is to consider reloading the entity from db instead of Attach. The reason behind is that you can't be sure that this entity exists in the database in the form that you are trying to attach it.

If this is not an option have you tried not Attaching the entity? If it already has the Entity Key set, it should update itself properly according to:

https://msdn.microsoft.com/en-us/data/jj592676.aspx

Talk1:
The problem even appears without attaching the entity. The problem doesn't appear when I don't change (add/remove) any remark.
转载于:https://stackoverflow.com/questions/30751935/updating-entity-with-many-to-many-relationship

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入