Cascade delete using Fluent API

前端 未结 1 1383
醉梦人生
醉梦人生 2020-12-06 14:38

I have two entities. Profile and ProfileImages. After fetching a Profile I want to delete ProfileImages through Pro

相关标签:
1条回答
  • 2020-12-06 15:01

    You can add this to your DB Context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Profile>()
        .HasOptional(c => c.ProfileImages)
        .WithOptionalDependent()
        .WillCascadeOnDelete(true);
    }
    

    Read more here:Enabling Cascade Delete

    You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.

    0 讨论(0)
提交回复
热议问题