实体框架多对多映射上的软删除 [英] Soft Delete on Entity Framework Many-to-Many Mapping

查看:45
本文介绍了实体框架多对多映射上的软删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多与以下示例类似的EF映射。我正在使用EF代码优先方法,因此我的映射类继承了EntityTypeConfiguration<>。

  this.HasMany(a => a。 KPI)
.WithMany()
.Map(a =>
{
a.ToTable( KeyResultArea_KeyPerformanceIndicator_Mapping);
a.MapLeftKey( KRA_Id );
a.MapRightKey( KPI_Id);
});

由于此原因,我留下了如下所示的架构。





<到目前为止,还没有什么惊喜。 -但是,我希望能够软删除这些映射之一,这样我的所需模式看起来应该像这样;

  dbo.KeyResultArea_KeyPerformanceIndicator_Mapping(
KRA_Id int,
KPI_Id int,
Deleted bit)

希望有道理,任何指针都将受到欢迎。

解决方案

我认为你可能需要具有自己的逻辑来定义关系的删除。您可以为关系定义新的实体类型,

 公共类KRAKPI {

public int KPA_Id { get; set;}
public int KRA_Id {get; set;}
public bool IsDeleted {get; set;}

}

然后您可以通过保存所有 KRAKPI 删除的项目来定义保存更改中的删除逻辑状态管理器,并通过更改 IsDeleted 值将其设置为修改状态。
此处为设置删除值的帖子在保存更改方法中。


I have a many to many EF map similar to the example below. I am using EF code first approach so my mapping class inherits EntityTypeConfiguration<>.

this.HasMany(a => a.KPIs)
            .WithMany()
            .Map(a =>
            {
                a.ToTable("KeyResultArea_KeyPerformanceIndicator_Mapping");
                a.MapLeftKey("KRA_Id");
                a.MapRightKey("KPI_Id");
            });

As a result of this im left with the schema shown below.

No great surprises so far. - However I would like to be able to soft delete one of these mappings so my desired schema would look something like this;

dbo.KeyResultArea_KeyPerformanceIndicator_Mapping(
   KRA_Id int,
   KPI_Id int,
   Deleted bit)

Hope it makes sense, any pointers would be most welcome.

解决方案

I think you may need to have your own logic to define the deletion of the relationships. You can define new enttity type for the relationship,

public class KRAKPI{

public int KPA_Id{get;set;}
public int KRA_Id{get;set;}
public bool IsDeleted{get;set;}

}

And then you can define the deletion logic in save changes by getting all KRAKPI deleted items in the state manager and setting them to modified state with changing the IsDeleted value. Here is a post on setting the deleted value in save changes method.

这篇关于实体框架多对多映射上的软删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆