Soft Delete on Entity Framework Many-to-Many Mapping

懵懂的女人 提交于 2019-12-11 00:06:51

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/11752265/soft-delete-on-entity-framework-many-to-many-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!