ChangeRelationshipState方法不支持使用外键值定义的关系The ChangeRelationshipState method is not supported for relationships that are defined by using foreign-key values
In EF 6, to remove entities from a collection without actually fetching the collection, I am using:
manager.ChangeRelationshipState(entity, wrappedIdOfRelatedEntity, e => e.Collection, EntityState.Deleted);
It works for M:N relationships. But in case of 1:N, it throws the following error:
The ChangeRelationshipState method is not supported for relationships that are defined by using foreign-key values.
Just to clarify, I do not want to fetch the collection, since it may be really huge, moreover I do not want to fetch the entities that should be removed.
I have the key (id) of the entity that 'contains' the collection and keys (ids) of the entities that should be removed.
Any hints, how to do it correctly?
NOTICE: Adding is possible by just adding wrapped entity keys to a collection. But removing is causing the problem.
Do you use row versioning/timestamp on your entities? If you do, EF needs to version because it's doing to send an update statement containing both the id and version of the entity to delete.
If you're not using row versioning/timestamp on your entities, you can do the following using the DbContext API but you need to use proxies and change detection.
- Recreate the entities you want to remove from the collection by using DbSet.Create
- Populate the recreated entities with the id of the entities you want to remove
- Attach the entities using DbSet.Attach
- Add the entities to the collection and then call AcceptAllChanges on the ObjectContext to reset the state of the entites
- Remove the entities from the collection
- Call save changes and it should issue update statements to remove the entities from the collection.
If you're using row versioning/timestamp on your entities, you can use the steps above, but in step 2, you need to set both the version and the id.
(原文：I cannot call AcceptAllChanges, since I am doing multiple changes at once. Is it possible to accept changes just per a single collection?)hjb417的回复:你能做重放(步骤1 - 4)之前您的更改吗?
(原文：can you do the replay (steps 1-4) before making your changes?)TN.的回复:这将使代码非常复杂,因为它是一个& # 39;api # 39;函数,它可以称为地方(例如,在一个复杂的事务)。
(原文：This would make the code really complex, since it is a 'API' function, which could be called anywhere (e.g. in a complex transactions).)
- Windowsphone网格——如何隐藏行和列? - Windows phone Grid - how hide row and column?
- 如何把一种特定的类在c# 4.0 LINQ查询对象How to cast a type to specific class in c# 4.0 after querying LINQ to Object
- 如何添加CRUD操作在WPF第二个实体How to Add CRUD Operations to a Second Entity in WPF
- Rhino-Mocking ExpectationViolationException
- enabled-properties多个控件绑定到相同的数据源属性Bind enabled-properties of multiple controls to the same DataSource property
- ChangeRelationshipState方法不支持使用外键值定义的关系The ChangeRelationshipState method is not supported for relationships that are defined by using foreign-key values
- 将DateTime从UTC时区的用户位置Convert DateTime from UTC to user location timezone
- 没有数据存在oledbdatareader行列no data exists for the row column in oledbdatareader
- CastleWindsor,通用存储库和两个数据库上下文CastleWindsor, Generic Repository and two database contexts
- 让管理员权限Keeping administrator privileges
- 显示数据表格中显示图像(多个图片) - Display Images in gridview (Multiple Images)