I have an application which works like this:
- EF loads data from the DB into POCOs (DAL layer)
- These EF POCOs are mapped up into equivalent objects (business layer)
- Changes are made to the business objects (business layer)
- Once we're ready to save, the business objects are mapped back down into EF objects (DAL layer)
Then when saving in the DAL layer, EF loads the existing DB objects, and these are replaced with the 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 I didn't include foreign keys in my business objects, as is frequently discouraged (business layer shouldn't need to care about DB relations), thus I get a mismatch between FK and object:
The property value(s) of 'Something.ID' on one end of a relationship do not match the property value(s) of 'RelatedThing.SomethingID' on the other end.
Am I forced to include FKs (and manage and update them) in this case, or is there a cleaner solution given this architecture?