EF 4.1 多对多删除父级应删除相关子级

发布于 2025-01-03 22:59:54 字数 959 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2025-01-10 22:59:54

您必须使用

DbContext.Users.Remove(user);

它与将状态设置为已删除不同。设置状态不会将任何具有级联删除设置的子对象标记为 Deleted,但 Remove 可以。

将状态设置为 Deleted 应该有效如果没有子项加载到上下文中,因为 EF 只会向数据库发送父项的 DELETE 语句,并且数据库将删除子项以及由于数据库中的级联删除。

如果但是您已将子级加载到上下文中,将父级的状态设置为已删除不会设置子级的状态。 EF 会抛出异常,而不是数据库抱怨。

You must use

DbContext.Users.Remove(user);

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 as Deleted but Remove 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.

﹏雨一样淡蓝的深情 2025-01-10 22:59:54

您应该能够指定删除角色或用户将依次删除子授权。您可以在流畅的 DbModelBuilder API 上使用 WillCascadeOnDelete() 方法:

modelBuilder.Entity<UserRoles>
    .HasRequired(d => d.User)
    .WithMany(p => p.UserRoles)
    .HasForeignKey(d => d.UserId)
    .WillCascadeOnDelete();

modelBuilder.Entity<Role>
    .HasMany(p => p.UserRoles)
    .WithRequired(d => d.Role)
    .HasForeignKey(d => d.RoleId)
    .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:

modelBuilder.Entity<UserRoles>
    .HasRequired(d => d.User)
    .WithMany(p => p.UserRoles)
    .HasForeignKey(d => d.UserId)
    .WillCascadeOnDelete();

modelBuilder.Entity<Role>
    .HasMany(p => p.UserRoles)
    .WithRequired(d => d.Role)
    .HasForeignKey(d => d.RoleId)
    .WillCascadeOnDelete();

With this setup, deleting a User or a Role should also delete all of the UserRoles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文