Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 让Automapper忽略EF中的非标量和nav属性_C#_Entity Framework_Automapper - Fatal编程技术网

C# 让Automapper忽略EF中的非标量和nav属性

C# 让Automapper忽略EF中的非标量和nav属性,c#,entity-framework,automapper,C#,Entity Framework,Automapper,需要automapper将域类型的属性从上下文映射回现有实体(基本上只是更新已更改的字段)。我需要它忽略导航属性,只映射标量属性 如果我说ForMember(o=>o.MyNavProperty,opt=>opt.Ignore),我就可以让它工作,但我更希望我的所有映射都有一个通用方法,告诉它只映射标量而不映射nav属性 尝试遵循毛里西奥的解决方案: 但我无法让它成功忽略我的导航属性 这是我的最新版本: private static void CreateMapForEF<T

需要automapper将域类型的属性从上下文映射回现有实体(基本上只是更新已更改的字段)。我需要它忽略导航属性,只映射标量属性

如果我说ForMember(o=>o.MyNavProperty,opt=>opt.Ignore),我就可以让它工作,但我更希望我的所有映射都有一个通用方法,告诉它只映射标量而不映射nav属性

尝试遵循毛里西奥的解决方案:

但我无法让它成功忽略我的导航属性

这是我的最新版本:

      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

                                       }));
      }
private static void createMapForf()
{
Mapper.CreateMap()
.ForAllMembers(o=>o.Condition(ctx=>
{
var members=ctx.Parent.SourceType.GetMember(ctx.MemberName);//获取我们映射的MemberInfo
如果(!members.Any())
返回false;
if(members.First().GetCustomAttributes(
typeof(EdmRelationshipNavigationPropertyAttribute),false)。Any()
返回false;
返回members.First().GetCustomAttributes(typeof(EdmScalarPropertyAttribute),false)。Any();//确定该成员是否设置了EdmScalar属性
}));
}

我使用一种显式方法,将接口添加到实体并映射到接口或从接口映射。因此,我不是在排除,而是在明确说明应该包括什么。通过声明分部类添加接口

该接口对我来说是免费的,因为我使用该接口进行解耦、测试存根、模拟等


也许只有我,但我不喜欢在AutoMapper配置中看到任何忽略。无法证明这一点,但我只是觉得不对。

我最近正在研究它。
您可以使用以下解决方案:

定义导航属性的属性:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class NavigationPropertyAttribute : Attribute
{
} 
使用上述属性标记视图模型中的所有导航属性

public class TagModel
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public string Description { get; set; }

    [Display(Name = "Image")]
    public string ImagePath { get; set; }

    [NavigationProperty]
    public List<ContentModel> Contents { get; set; }
}
public static IMappingExpression<TSource, TDestination> IgnoreNavigationProperties<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
    var sourceType = typeof(TSource);

    foreach (PropertyInfo property in sourceType.GetProperties())
    {
        var isNavProp = property.GetCustomAttributes(typeof(NavigationPropertyAttribute), false).Count() == 1;
        if (isNavProp)
                expression.ForMember(property.Name, opt => opt.Ignore());
    }
    return expression;
}
最后,您可以按如下方式使用它:

Mapper.CreateMap<TagModel, Tag>()
        .ForMember(m => m.Id, opt => opt.Condition(m => m.Id > 0))
        .IgnoreNavigationProperties();
Mapper.CreateMap()
.ForMember(m=>m.Id,opt=>opt.Condition(m=>m.Id>0))
.IgnoreNavigationProperties();

No忽略?那么你如何处理用户不允许更新的列呢?+1我同意。这种自动映射有一天会让你头疼,而且很难从代码中看出发生了什么。从接口到实体类的映射是保持控制的最佳方式。如果用户不应该更新列,不要将它们放在界面中,但这也是视图模型/验证要处理的事情。这是一件更令人好奇的事情,看看是否有可能做到这一点,有些人发布了类似于我上面发布的解决方案,但我无法让它们工作。我知道我可以显式地设置映射,我会一直这样做,直到我可以让它工作。。。仅供参考。。。就像我做的那样。让AutoMapper做它的名字意味着什么。在映射对象的代码中定义契约。我的意见是,AutoMapper文件应该只是将这个映射到这个。否则你会得到部分映射的对象。贝蒂-你不必。这是一件好事,您可以通过代码契约进行映射。如果它不在接口上,则它不是服务的一部分。使用接口类型而不是具体类型来定义对象“是什么”。