软删除 EF Core 中的中间 Table 行
Soft Delete Intermediate Table Row In EF Core
我需要在多对多关系中使用软删除关系。
public class EntityA
{
public int Id {get; set;}
public ICollection<EntityB> BCollection {get; set;}
public bool IsDeleted {get; set;}
}
public class EntityB
{
public int Id {get; set;}
public ICollection<EntityA> ACollection {get; set;}
public bool IsDeleted {get; set;}
}
我可以使用 fluent API.
为多对多关系配置中间 table
modelBuilder.Entity<EntityA>()
.HasMany<EntityB>(s => s.BCollection)
.WithMany(c => c.ACollection)
.Map(cs =>
{
cs.MapLeftKey("AId");
cs.MapRightKey("BId");
cs.ToTable("ABRelationships");
});
我数据库中的所有实体都使用 'IsDeleted' 标志来使用软删除,我正在使用全局查询过滤器来阻止这些实体在正常查询中返回。
modelBuilder.Entity<EntityA>().HasQueryFilter(x => x.IsDeleted);
modelBuilder.Entity<EntityB>().HasQueryFilter(x => x.IsDeleted);
我如何在中间 table 周围添加一个查询过滤器,而不是从中间删除行 table 一个 IsDeleted 标志被设置并且在查看我的时它不会自动加入人际关系?
Svyatoslav 的评论让我找到了正确的答案,即利用第三方实体并创建一个使用实体。
首先,我为 ABRelationship
添加了一个新实体 class
public class ABEntity
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
[Required]
[ForeignKey(nameof(EntityA))]
public int EntityAId { get; set; }
public EntityA EntityA { get; set; }
[Required]
[ForeignKey(nameof(EntityB))]
public int EntityBId { get; set; }
public EntityA EntityA { get; set; }
}
然后我添加了新的集合属性。我将它们设置为在使用过滤器的序列化中被忽略,因此关系实体未映射到响应。
public class EntityA
{
public int Id {get; set;}
public ICollection<EntityB> BCollection {get; set;}
public bool IsDeleted {get; set;}
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<EntityAB> EntityAB{ get; set; }
}
public class EntityB
{
public int Id {get; set;}
public ICollection<EntityA> ACollection {get; set;}
public bool IsDeleted {get; set;}
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<EntityAB> EntityAB{ get; set; }
}
然后我使用 'UsingEntity' 扩展来映射我的关系实体。
modelBuilder.Entity<EntityA>()
.HasMany(p => p.BCollection)
.WithMany(p => p.ACollection)
.UsingEntity<ABRelationshipEntity>(
j => j
.HasOne(pt => pt.EntityA)
.WithMany(t => t.EntityAB)
.HasForeignKey(pt => pt.EntityAId),
j => j
.HasOne(pt => pt.EntityB)
.WithMany(p => p.EntityAB)
.HasForeignKey(pt => pt.EntityBID));
一旦我设置了上面的内容,下面的全局过滤器就可以正确地过滤掉软删除的关系。
modelBuilder.Entity<ABRelationshipEntity>().HasQueryFilter(x => x.IsDeleted);
我需要在多对多关系中使用软删除关系。
public class EntityA
{
public int Id {get; set;}
public ICollection<EntityB> BCollection {get; set;}
public bool IsDeleted {get; set;}
}
public class EntityB
{
public int Id {get; set;}
public ICollection<EntityA> ACollection {get; set;}
public bool IsDeleted {get; set;}
}
我可以使用 fluent API.
为多对多关系配置中间 tablemodelBuilder.Entity<EntityA>()
.HasMany<EntityB>(s => s.BCollection)
.WithMany(c => c.ACollection)
.Map(cs =>
{
cs.MapLeftKey("AId");
cs.MapRightKey("BId");
cs.ToTable("ABRelationships");
});
我数据库中的所有实体都使用 'IsDeleted' 标志来使用软删除,我正在使用全局查询过滤器来阻止这些实体在正常查询中返回。
modelBuilder.Entity<EntityA>().HasQueryFilter(x => x.IsDeleted);
modelBuilder.Entity<EntityB>().HasQueryFilter(x => x.IsDeleted);
我如何在中间 table 周围添加一个查询过滤器,而不是从中间删除行 table 一个 IsDeleted 标志被设置并且在查看我的时它不会自动加入人际关系?
Svyatoslav 的评论让我找到了正确的答案,即利用第三方实体并创建一个使用实体。
首先,我为 ABRelationship
添加了一个新实体 classpublic class ABEntity
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
[Required]
[ForeignKey(nameof(EntityA))]
public int EntityAId { get; set; }
public EntityA EntityA { get; set; }
[Required]
[ForeignKey(nameof(EntityB))]
public int EntityBId { get; set; }
public EntityA EntityA { get; set; }
}
然后我添加了新的集合属性。我将它们设置为在使用过滤器的序列化中被忽略,因此关系实体未映射到响应。
public class EntityA
{
public int Id {get; set;}
public ICollection<EntityB> BCollection {get; set;}
public bool IsDeleted {get; set;}
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<EntityAB> EntityAB{ get; set; }
}
public class EntityB
{
public int Id {get; set;}
public ICollection<EntityA> ACollection {get; set;}
public bool IsDeleted {get; set;}
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<EntityAB> EntityAB{ get; set; }
}
然后我使用 'UsingEntity' 扩展来映射我的关系实体。
modelBuilder.Entity<EntityA>()
.HasMany(p => p.BCollection)
.WithMany(p => p.ACollection)
.UsingEntity<ABRelationshipEntity>(
j => j
.HasOne(pt => pt.EntityA)
.WithMany(t => t.EntityAB)
.HasForeignKey(pt => pt.EntityAId),
j => j
.HasOne(pt => pt.EntityB)
.WithMany(p => p.EntityAB)
.HasForeignKey(pt => pt.EntityBID));
一旦我设置了上面的内容,下面的全局过滤器就可以正确地过滤掉软删除的关系。
modelBuilder.Entity<ABRelationshipEntity>().HasQueryFilter(x => x.IsDeleted);