[英]Automapper - ignoring while mapping
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 an EntityKey
go get an item from EDM. 忽略CreateEntityKey
部分-它只是创建一个EntityKey
并从EDM获取项目。 ce
is of type ObjectContext
. ce
是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. 我想做的是尝试从EDM获取一个实体,使用AutoMapper更新它,或者如果不在其中,则使用AutoMapper创建它。
Problem is that AutoMapper maps all properties - even primary keys that are not allowed to be changed by edm and I get a nasty exception. 问题是AutoMapper会映射所有属性-甚至是edm不允许更改的主键,而且我得到了一个讨厌的异常。 I can ignore PKs in CreateMap
, but then I'll not be able to do a 2nd part with creation. 我可以忽略CreateMap
PK,但是之后我将无法进行创建的第二部分。 I get PKs from elsewhere... 我从其他地方获得PK ...
One thing I may consider is something in CreateMap
- some conditional ignore on destination object or actually used method. 我可能会考虑的一件事是CreateMap
某些内容-对目标对象或实际使用的方法的一些条件忽略。 Another is to apply some ignoring properties in Mapper.Map
method, or manually mapping like so: 另一个方法是在Mapper.Map
方法中应用一些忽略属性,或者像这样手动映射:
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? 问题很简单:我该怎么办? :) :)
忽略PK然后只做c.YourPK =第二部分的某个值怎么样?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.