EF 4.1 多对多删除父级应删除相关子级
EF 4.1 数据库优先方法。
假设我有这个表架构
用户 1---M 用户角色 M---1 角色
级联删除是在外键中设置的
UserRoles 表具有其他列,例如 CreatedDate,因此我为 UserRoles 创建一个模型并进行相应的映射。
我最终得到以下模型:
User
----
int Id
string Name
List<UserRoles> UserRoles
UserRoles
---------
int UserId
int RoleId
DateTime CreatedDate
User User
Role Role
Role
----
int Id
string Name
List<UserRoles> UserRoles
如果我的配置正确,我是否应该能够删除用户,并且用户角色行是否会被删除而无需手动清除 UserRoles 集合?
那么我可以这样做吗:
DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();
或者我是否必须这样做:
user.UserRoles.Clear();
DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();
我的测试显示我必须清除子集合,但我发现相互冲突的信息,如果我正确地进行了级联删除设置,则它应该仅通过删除用户来工作。
当我不清除 UserRoles 时,我收到此错误:
无法更改关系,因为一个或多个 外键属性不可为空
感谢您帮助澄清这一点!
EF 4.1 Database First approach.
Say I have this table schema
Users 1---M UserRoles M---1 Roles
Cascade delete is setup in the Foreign Keys
The UserRoles table has additional columns like CreatedDate so I create a model for UserRoles and map accordingly.
I end up with the following Models:
User
----
int Id
string Name
List<UserRoles> UserRoles
UserRoles
---------
int UserId
int RoleId
DateTime CreatedDate
User User
Role Role
Role
----
int Id
string Name
List<UserRoles> UserRoles
If I have my configuration correct, should I be able to delete a user and will the user roles rows be deleted WITHOUT having to clear the UserRoles collection manually?
So can I just do this:
DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();
Or do I HAVE to do this:
user.UserRoles.Clear();
DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();
My testing shows I HAVE to clear the child collection, but I find conflicting information that if I have cascade delete setup correctly it should work by only deleting the User.
When I DON'T clear the UserRoles I receive this error:
The relationship could not be changed because one or more of the
foreign-key properties is non-nullable
Thanks for you help in clarifying this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用
它与将状态设置为
已删除
不同。设置状态不会将任何具有级联删除设置的子对象标记为Deleted
,但Remove
可以。将状态设置为
Deleted
应该有效如果没有子项加载到上下文中,因为 EF 只会向数据库发送父项的 DELETE 语句,并且数据库将删除子项以及由于数据库中的级联删除。如果但是您已将子级加载到上下文中,将父级的状态设置为
已删除
不会设置子级的状态。 EF 会抛出异常,而不是数据库抱怨。You must use
It is not the same thing as setting the state to
Deleted
. Setting the state won't mark any child objects with cascading delete setup asDeleted
butRemove
will do.Setting the state to
Deleted
should work IF no children are loaded into the context because EF will send only a DELETE statement for the parent to the database and the database will delete the children as well due to the cascading delete in the database.IF however you have loaded children into the context setting the state on the parent to
Deleted
won't set the state of the children. EF will throw the exception, it's not the database who complains.您应该能够指定删除角色或用户将依次删除子授权。您可以在流畅的 DbModelBuilder API 上使用 WillCascadeOnDelete() 方法:
通过此设置,删除用户或角色也应该删除所有 UserRoles。
You should be able to specify that deleting a Role or User will in turn delete the child grants. You can use the WillCascadeOnDelete() method on the fluent DbModelBuilder API:
With this setup, deleting a User or a Role should also delete all of the UserRoles.