欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EntityFrameworkCore5知识点简要 - 删除数据

程序员文章站 2023-12-28 11:56:22
...

软删除: soft delete ,  标记为删除

硬删除:   hard delete,  从表里删除

DeleteBehavior:

Cascade Delete: 级联删除  默认

modelBuilder.Entity<Person>(entity =>
{
    entity.HasMany(x => x.Addresses)
    .WithOne(x => x.Person)
    .OnDelete(DeleteBehavior.Cascade);
});

上面代码也定义了一对多的关系

Client Set Null: 设为null

modelBuilder.Entity<Person>().HasMany(x => x.Addresses)
   .WithOne(x => x.Person)
   .HasForeignKey(x=>x.PersonId)
   .OnDelete(DeleteBehavior.ClientSetNull);

实现上面操作,需指定外键字段为 可空类型  ?

public int? PersonId { get; set; }

Set Null: 设为null

同上,但是不用我们设置为null

限制删除  Restrict Delete

modelBuilder.Entity<Person>(entity =>
{
    entity.HasMany(x => x.Addresses)
    .WithOne(x => x.Person)
    .HasForeignKey(x => x.PersonId)
    .OnDelete(DeleteBehavior.Restrict);
});

上一篇:

下一篇: