C# automapper inherting配置文件在json中返回额外的属性

C# automapper inherting配置文件在json中返回额外的属性,c#,.net-core,automapper,C#,.net Core,Automapper,据我所知,标准是继承Profile类,以便Automapper检测地图 我这样做对吗?有没有办法忽略这些额外属性?您的模型不应该继承配置文件。您可以将Profile子类化以配置模型到模型的映射 public class GamerMappingProfile : Profile { public GamerMappingProfile() { CreateMap<Gamer, GamerVM>(); CreateMap<Gamer

据我所知,标准是继承Profile类,以便Automapper检测地图


我这样做对吗?有没有办法忽略这些额外属性?

您的模型不应该继承配置文件。您可以将Profile子类化以配置模型到模型的映射

public class GamerMappingProfile : Profile
{
    public GamerMappingProfile()
    {
        CreateMap<Gamer, GamerVM>();
        CreateMap<GamerVM, Gamer>();
    }
}

您是否尝试了JSON ignore属性?您的模型不应继承配置文件。您可以将Profile子类化以配置模型到模型的映射。啊,我明白了,我有一个可移植的界面,我一直在用我的方式为早期版本的automapper进行操作。我以为这个档案课也是这么做的。我更喜欢我的方式:(.Imo,映射应该是类的一部分,而不是其他地方。但是谢谢!我需要逐个添加所有配置文件,还是只能自动检测配置文件类?我一直在加载我的配置文件。我还没有研究自动检测我的配置。那真是一团糟,我的一些应用程序有50多个域模型和3个不同的视图模型。lol…。我想我是的我将坚持使用现有的基础结构.ClassFinder.GetEnumerableOfType().Each(c=>c.Map());
 {
    "id": 8,
    "name": "Ashton Heir",
    "tag": "Legend",
    "defaultMemberConfig": {
        "nameMapper": {
            "getMembers": {},
            "namedMappers": [
                {
                    "methodCaseSensitive": false
                },
                {},
                {
                    "prefixes": [
                        "Get"
                    ],
                    "postfixes": [],
                    "destinationPrefixes": [],
                    "destinationPostfixes": []
                }
            ]
        },
        "memberMappers": [
            {
                "nameMapper": {
                    "getMembers": {},
                    "namedMappers": [
                        {
                            "methodCaseSensitive": false
                        },
                        {},
                        {
                            "prefixes": [
                                "Get"
                            ],
                            "postfixes": [],
                            "destinationPrefixes": [],
                            "destinationPostfixes": []
                        }
                    ]
                }
            },
            {
                "sourceMemberNamingConvention": {
                    "splittingExpression": {
                        "pattern": "(\\p{Lu}+(?=$|\\p{Lu}[\\p{Ll}0-9])|\\p{Lu}?[\\p{Ll}0-9]+)",
                        "options": 0
                    },
                    "separatorCharacter": ""
                },
                "destinationMemberNamingConvention": {
                    "splittingExpression": {
                        "pattern": "(\\p{Lu}+(?=$|\\p{Lu}[\\p{Ll}0-9])|\\p{Lu}?[\\p{Ll}0-9]+)",
                        "options": 0
                    },
                    "separatorCharacter": ""
                }
            }
        ]
    }
public class GamerMappingProfile : Profile
{
    public GamerMappingProfile()
    {
        CreateMap<Gamer, GamerVM>();
        CreateMap<GamerVM, Gamer>();
    }
}
var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<GamerMappingProfile>();
    cfg.AddProfile<MyOtherProfile>();
});

var mapper = config.CreateMapper();
// Scan for all profiles in an assembly
// ... using instance approach:
var config = new MapperConfiguration(cfg => {
    cfg.AddProfiles(myAssembly);
});
// ... or static approach:
Mapper.Initialize(cfg => cfg.AddProfiles(myAssembly));

// Can also use assembly names:
Mapper.Initialize(cfg =>
    cfg.AddProfiles(new [] {
        "Foo.UI",
        "Foo.Core"
    });
);

// Or marker types for assemblies:
Mapper.Initialize(cfg =>
    cfg.AddProfiles(new [] {
        typeof(HomeController),
        typeof(Entity)
    });
);