简体   繁体   English

使用多对多映射更新分离的EF对象

[英]Updating detached EF object with many to many mapping

I have a repository in EF that needs to Update detached entities but ones which exist in the ObjectStateManager. 我在EF中有一个存储库,需要更新分离的实体,但需要更新ObjectStateManager中存在的实体。 Imagine you have a session open on a DBContext where you have loaded a particular entity, but then you receive a request to update from a detached version of the object. 想象一下,在已加载特定实体的DBContext上打开了一个会话,但是随后您收到了从对象的分离版本中进行更新的请求。 The only way I have found to do this is to GET the existing object in the state manager, then one by one, update the fields to the fields of the passed-in object. 我发现这样做的唯一方法是在状态管理器中获取现有对象,然后一个接一个地将字段更新为传入对象的字段。 Then set the state of the object state manager version to modified, and save the context. 然后将对象状态管理器版本的状态设置为已修改,并保存上下文。

This works for simple entities that don't contain navigation properties. 这适用于不包含导航属性的简单实体。

I am now trying to do so on an entity that has a many-to-many relationship. 我现在正在尝试对具有多对多关系的实体执行此操作。

Imagine you have a BlogPost object, and a Hashtag object. 假设您有一个BlogPost对象和一个Hashtag对象。 This is a many to many relationship. 这是多对多的关系。 I have defined this and in the database I can see I have three tables, the BlogPost, the HashTag and the mapping table. 我已经定义了这个,在数据库中我可以看到我有三个表,BlogPost,HashTag和映射表。

What I want to be able to do is edit the blog post on the front end, pass in the updated blog post with it's new list of hashtags that apply to it, and update the database. 我想要做的是在前端编辑博客文章,将更新的博客文章及其适用的新的#标签列表传递给它,并更新数据库。

The problem is the list of hashtags could be completely unrelated to the old one, so I first have to clear out all the previous mappings, then add the new ones in. If they are the same, this will be a necessary redundancy but the only way to achieve it. 问题是#标签列表可能与旧标签列表完全不相关,因此我首先必须清除所有以前的映射,然后再添加新的。如果它们相同,这将是必要的冗余,但是唯一实现它的方法。

I cannot figure out how to clear out the previous mappings in the many to many relationship though. 我无法弄清楚如何清除多对多关系中的先前映射。 I have tried 我努力了

foreach (var tag in dbBlogPost.Hashtags)
    dbItem.Hashtags.Remove(tag);

I then add the new hashtags to the empty collection, then do 然后,我将新的标签添加到空集合中,然后执行

Work.Context.Entry(dbItem).State = EntityState.Modified;
Work.Context.Save();

But when I save the repository, I get the following exception 但是当我保存存储库时,出现以下异常

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 When a change is made to a relationship, the related foreign-key property is set to a null value. 对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

Can anyone suggest what I am doing wrong? 谁能暗示我在做什么错?

The DbSet<TEntity>.Local represents a local view of all Added, Unchanged, and Modified entities in this set. DbSet<TEntity>.Local表示此集中所有已添加,未更改和已修改实体的本地视图。 So you can clear all the collection before make any change. 因此,您可以在进行任何更改之前清除所有集合。

dbBlogPost.Hashtags.Local.Clear();

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

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