views:

119

answers:

1

Is it possible to ignore depending on destination value?

look what i want to do:

  object c;
  var key = ce.CreateEntityKey<CustomerDataContract, Customer>("FullCustomerSet", item, o => o.ID);

  if (ce.TryGetObjectByKey(key, out c))
  {
      Mapper.Map(item, (Customer)c);
  }
  else
  {
      c = Mapper.Map<CustomerDataContract, Customer>(item);
      ce.AddObject("FullCustomerSet", c);
  }

Ignore the CreateEntityKey part - it's just creating EntityKey go get an item from EDM. ce is ObjectContext.

What I want to do is try to get an entity from edm, update it with automapper or if it's not in there create it with automapper.

Propblem is that automapper maps all properties - even primary keys that are not allowed to be changed by edm and I get a nasty exception. I can ignore PKs in CreateMap, but then I'll not be able to do a 2nd part with creation. I get PKs from elsewhere...

One thing I may consider is something in CreateMap - some conditional ignore on destination object or actually used method. Another is to apply some ignoring properties in Mapper.Map method, or manually mapping like so:

var map = Mapper.FindTypeMapFor<TSource, TDestination>();
foreach (var item in map.GetPropertyMaps().OrderBy(o=>o.GetMappingOrder()))
{
    if (ignore.Contains(item.DestinationProperty.Name))
    {
        continue;
    }
    //and now I should do some mapping, but don't know how...
}

Question is simple: How can I do it? :)

Regards, Piotr

A: 

How about ignoring PK and then just doing c.YourPK = somevalue in the secong part?

epitka
yeap... similar idea. I just create or get it from store and than only apply mapping on it ignoring PK. Thanx
WooBoo