简体   繁体   English

EF CORE-Fluent API-级联删除限制到表

[英]EF CORE - Fluent API - cascading delete restrict to on table

The model: 该模型:

public class AccountUser
{
    public long AccountUserId { get; set; }
    ...
    public long UserPermissionGroupId { get; set; }
    public UserPermissionGroup UserPermissionGroup { get; set; }
}

public class UserPermissionGroup
{
    public long UserPermissionGroupId { get; set; }
    ...
    public string Name { get; set; }
}

The question: How do I set foreign key on table AccountUser->UserPermissionGroup to restrict on delete? 问题:如何在表AccountUser-> UserPermissionGroup上设置外键以限制删除?

I'm unable to find example how to set foreign key on delete action to restrict to only one table. 我找不到如何在删除操作上将外键设置为仅限于一个表的示例。 I can not use something like WithMany/WithOne and then OnDelete as in this example https://docs.microsoft.com/en-us/ef/core/modeling/relationships#cascade-delete-1 because I have no reference from UserPermissionGroup back to AccountUser. 在此示例中,我不能使用WithMany / WithOne之类的东西,然后再使用OnDelete之类的东西https://docs.microsoft.com/zh-cn/ef/core/modeling/relationships#cascade-delete-1,因为我没有UserPermissionGroup的引用回到AccountUser。

Thank you very much. 非常感谢你。

In your fluent API you can do something like this: 在流畅的API中,您可以执行以下操作:

builder.Entity<AccountUser>()
    .HasOne(a => a.UserPermissionGroup)
    .WithOne().OnDelete(DeleteBehavior.Restrict);

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

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