问题
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