需要使用自动映射器将一个域类型的属性映射回上下文中的现有实体(基本上只更新已更改的字段)。我需要它忽略导航属性,只映射标量属性。
如果我说 ForMember(o => o.MyNavProperty, opt => opt.Ignore),我可以让它工作,但我宁愿为所有映射使用通用方法,告诉它只映射标量而不是 nav 属性。
试图遵循 Mauricio 的解决方案: ASP.net MVC - Should I use AutoMapper from ViewModel to Entity Framework entities? 但我无法成功地忽略我的导航属性。
这是我的更新版本:
如果我说 ForMember(o => o.MyNavProperty, opt => opt.Ignore),我可以让它工作,但我宁愿为所有映射使用通用方法,告诉它只映射标量而不是 nav 属性。
试图遵循 Mauricio 的解决方案: ASP.net MVC - Should I use AutoMapper from ViewModel to Entity Framework entities? 但我无法成功地忽略我的导航属性。
这是我的更新版本:
private static void CreateMapForEF<TDto, TEntity>()
{
Mapper.CreateMap<TDto, TEntity>()
.ForAllMembers(o => o.Condition(ctx =>
{
var members = ctx.Parent.SourceType.GetMember(ctx.MemberName); // get the MemberInfo that we are mapping
if (!members.Any())
return false;
if (members.First().GetCustomAttributes(
typeof (EdmRelationshipNavigationPropertyAttribute), false).Any())
return false;
return members.First().GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).Any(); // determine if the Member has the EdmScalar attribute set
}));
}