简体   繁体   English

软删除 EF Core 中的中间表行

[英]Soft Delete Intermediate Table Row In EF Core

I have a need to use a soft delete of a relationship in a many to Many Relationship.我需要对多对多关系中的关系进行软删除。

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;}
}

I can configure an intermediate table for the many to many relationship using the fluent API.我可以使用 fluent API 为多对多关系配置一个中间表。

modelBuilder.Entity<EntityA>()
            .HasMany<EntityB>(s => s.BCollection)
            .WithMany(c => c.ACollection)
            .Map(cs =>
                    {
                        cs.MapLeftKey("AId");
                        cs.MapRightKey("BId");
                        cs.ToTable("ABRelationships");
                    });

All the entities in my database use an 'IsDeleted' flag to use soft deletion and I am making use of a global query filter to stop these entities from being returned on a normal query.我数据库中的所有实体都使用“IsDeleted”标志来使用软删除,我正在使用全局查询过滤器来阻止这些实体在正常查询中返回。

        modelBuilder.Entity<EntityA>().HasQueryFilter(x => x.IsDeleted);
        modelBuilder.Entity<EntityB>().HasQueryFilter(x => x.IsDeleted);

How would i add a query filter around the intermediate table so that instead of deleting the row from the intermediate table an IsDeleted flag is set and it is not automatically joined when looking at my relationships?我将如何在中间表周围添加查询过滤器,以便不是从中间表中删除行,而是设置 IsDeleted 标志,并且在查看我的关系时不会自动加入?

The comment by Svyatoslav lead me to the correct answer which is to make use of a third entity and create a using entity. Svyatoslav 的评论使我得到了正确的答案,即利用第三个实体并创建一个 using 实体。

First I added a new entity class for the ABRelationship首先,我为 ABRelationship 添加了一个新的实体类

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; }

}

Then I added the new collection properties.然后我添加了新的集合属性。 I set them to be ignored from serialization with filters so the relationship entities where not mapped to responses.我将它们设置为从使用过滤器的序列化中忽略,因此关系实体未映射到响应。

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; }
}

I then used the 'UsingEntity' extension to map my relationship entity.然后我使用“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));

Once I set the above the following global filter works to correctly filter out soft deleted relationships.一旦我设置了上述内容,以下全局过滤器就可以正确过滤掉软删除的关系。

modelBuilder.Entity<ABRelationshipEntity>().HasQueryFilter(x => x.IsDeleted);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM