4

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?

T.S.
  • 18,195
  • 11
  • 58
  • 78
FBryant87
  • 4,273
  • 2
  • 44
  • 72
  • Does your mapper overwrites the foreign keys? – Dido Aug 04 '17 at 15:51
  • Could `[NotMapped]` attribute be what you're looking for? – webnoob Aug 04 '17 at 15:51
  • How did `ID` became different? If the objects perfectly equivalent, could you use hydrator instead of mapper? – T.S. Aug 04 '17 at 15:52
  • The Business Layer objects have no FK properties, so after the mapping back to EF objects, the foreign keys are wiped (to zero). Preventing the FKs from being overwritten with a tag could perhaps be a solution - I'll test. – FBryant87 Aug 04 '17 at 15:57
  • If you set `NotMapped` oh your EF model, you will be not getting data from DB. We use simple home-written (5 lines) hydrator that only copies data from properties to properties that have same name and data type. So, if your property is not listed in source model, the target model property will not be wiped out. What mapper is it anyway? – T.S. Aug 04 '17 at 16:03
  • Here is your answer, I think https://stackoverflow.com/questions/4987872/ignore-mapping-one-property-with-automapper. If you write to specific user, don't forget to include tag -> @FBryant87 – T.S. Aug 04 '17 at 16:04
  • This issue is the reason I've decided not to use AutoMapper for BIZ -> DAL mappings. The objects in these layers are usually different enough that it makes for complicated mapping definitions. While it's possible to configure your way around it, i've found it's much easier to just map the objects the old fashioned way (assigning properties). – echo Aug 04 '17 at 16:07
  • @T.S. That looks very good, though having to write Ignore for every FK would become a nightmare - perhaps there's a way tell AutoMapper to ignore properties which don't exist on the source object? (business) - similar to Stéphane's answer. – FBryant87 Aug 04 '17 at 16:13
  • 1
    This? https://stackoverflow.com/questions/4367591/how-to-ignore-all-destination-members-except-the-ones-that-are-mapped – T.S. Aug 04 '17 at 16:16

0 Answers0