EF6 - How to avoid including foreign keys in models when matching?

I have an application that works like this:

  • EF loads data from DB into POCOs (DAL layer)
  • These EF POCOs map to equivalent objects (business layer)
  • Changes to business objects (business layer)
  • Once we are ready to save, the business objects are mapped back to EF objects (DAL layer)

Then, when saved to the DAL layer, EF loads the existing DB objects and replaces them with equivalent (potentially changed) business objects:

var thing = context.Things.First(s => s.ID == ID)
thing = Mapper.Map(changedThing, thing);
context.Entry(thing).State = EntityState.Modified

      

The problem is that I have not included foreign keys in my business objects, as it is often discouraged (the business layer should not care about DB connections), so I get a mismatch between the FK and the object:

The value (s) of the "Something.ID" property at one end of the relationship does not match the values โ€‹โ€‹of the "RelatedThing.SomethingID" property at the other end.

Am I forced to enable (and manage and update) FKs in this case, or is there a cleaner solution given this architecture?

+3


source to share





All Articles