2

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.

enter image description here

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.

Christo
  • 2,330
  • 3
  • 24
  • 37

1 Answers1

2

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.

Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • Yeah this is the only way ive found to do this – undefined Aug 01 '12 at 03:50
  • hmmm ok, so just to I understand and so I can map this. The domain model relationship would look something like KRA.KRAKPI.KPIs.FirstOrDefault().Name where previously I had KRA.KPIs.FirstOrDefault().Name? Thanks for your reply – Christo Aug 01 '12 at 05:27