EFCore nullable relationship setting onDelete: ReferentialAction.Restrict

后端 未结 1 506
迷失自我
迷失自我 2020-12-10 03:13

I\'m running efcore 2.0.1.

I have a model:

public class BigAwesomeDinosaurWithTeeth
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identi         


        
相关标签:
1条回答
  • 2020-12-10 03:55

    EF Core 2.0.1 metadata and migrations use different enums for specifying the delete behavior - respectively DeleteBehavior and ReferentialAction. While the first is well documented, the second and the mapping between the two is not (at the time of writing).

    Here is the current mapping:

    DeleteBehavior    ReferentialAction
    ==============    =================
    Cascade           Cascade
    ClientSetNull     Restrict
    Restrict          Restrict
    SetNull           SetNull
    

    In your case, the relationship is optional, hence the DeleteBehavior by convention is ClientSetNull which maps to onDelete: Restrict, or in other words, enforced (enabled) FK w/o cascade delete.

    If you want different behavior, you have to use fluent API, e.g.

    modelBuilder.Entity<BigAwesomeDinosaurWithTeeth>()
        .HasMany(e => e.YummyPunyPrey)
        .WithOne(e => e.BigAwesomeDinosaurWithTeeth)
        .OnDelete(DeleteBehavior.SetNull); // or whatever you like
    
    0 讨论(0)
提交回复
热议问题