automapper - simplest option to only write to destination property if the source property is different? -


note: scenario using 2 entity framework models sync data between 2 databases, i'd imagine applicable other scenarios. 1 try tackling on ef side (like in this question) wanted see if automapper handle out-of-the-box

i'm trying figure out if automapper can (easily :) compare source , dest values (when using sync existing object) , copy if values different (based on equals default, potentially passing in func, if decided string.equals stringcomparison.ordinalignorecase particular pair of values). @ least scenario, i'm fine if it's restricted tsource == tdest case (i'll syncing on int's, string's, etc, don't think i'll need type converters involved)

looking through samples , tests, closest thing seems conditional mapping (src\unittests\conditionalmapping.cs), , use condition overload takes func (since other overload isn't sufficient, need dest information too). looks on surface work fine (i haven't used yet), end specifying every member (although i'm guessing define small number of actions/methods , @ least reuse them instead of having n different lambdas).

is simplest available route (outside of changing automapper) getting 'only copy if source , dest values different' or there way i'm not seeing? if is simplest route, has been done before elsewhere? feels i'm reinventing wheel here. :)

chuck norris (formerly known omu? :) answered this, via comments, answering , accepting repeat said.

@james manning have inherit conventioninjection, override match method , write there return c.sourceprop.name = c.targetprop.name && c.sourceprop.value != c.targetprop.value , after use target.injectfrom(source);

in particular case, since had couple of other needs anyway, customized ef4 code generation include check whether new value same current value (for scalars) takes care of issue doing 'conditional' copy - can use automapper or valueinject or whatever as-is. :)

for interested in change, when default *.tt file, simplest way make change (at least tell) find 2 lines like:

if (ef.iskey(primitiveproperty)) 

and change both like:

if (ef.iskey(primitiveproperty) || true) // want setter include checking target value being set 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -