2

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?

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://learn.microsoft.com/en-us/ef/core/modeling/relationships#cascade-delete-1 because I have no reference from UserPermissionGroup back to AccountUser.

Thank you very much.

ferdinand
  • 970
  • 1
  • 7
  • 14

1 Answers1

10

In your fluent API you can do something like this:

builder.Entity<AccountUser>()
    .HasOne(a => a.UserPermissionGroup)
    .WithOne().OnDelete(DeleteBehavior.Restrict);
i regular
  • 584
  • 6
  • 16