C# 找到自动映射未映射成员

C# 找到自动映射未映射成员,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,在这里发疯 型号: public class CountryModel { public int Id { get; set; } [Required (ErrorMessage = "A value is required")] [StringLength(30, MinimumLength = 4, ErrorMessage = "Minimum value is 4 characters")] public string Name { get; set;

在这里发疯

型号:

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

    [Required (ErrorMessage = "A value is required")]
    [StringLength(30, MinimumLength = 4, ErrorMessage = "Minimum value is 4 characters")]
    public string Name { get; set; }
}
实体框架数据库模型

public partial class Country
{
    public Country()
    {
        this.City = new HashSet<City>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<City> City { get; set; }
}
公共部分类国家/地区
{
公共国家()
{
this.City=new HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection城市{get;set;}
}
制图员:

public class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Country, CountryModel>();
                cfg.CreateMap<CountryModel, Country>().ForMember(x=> x.City, opt => opt.Ignore());
            }
        );
    }
}
公共类自动映射器配置
{
公共静态void Configure()
{
Mapper.Initialize(cfg=>
{
CreateMap();
cfg.CreateMap().formMember(x=>x.City,opt=>opt.Ignore());
}
);
}
}
在城市中失败。。。我做错了什么

编辑:使用Automapper V7.0.1

编辑2:

像这样映射:

    public int AddCountry(CountryModel model)
    {
        var mappedC = _mapper.Map<CountryModel, Country>(model);

        int countryId = _locationManager.AddCountry(mappedC);

        return 1;
    }
public int AddCountry(CountryModel)
{
var mappedC=_mapper.Map(模型);
int countryId=\u locationManager.AddCountry(mappedC);
返回1;
}
我正在从countrymodel(源)映射到国家(目标)

编辑3:

错误消息:

找到了未映射的成员。查看下面的类型和成员。添加 自定义映射表达式、忽略、添加自定义解析器或修改 无匹配构造函数的源/目标类型,请添加无参数 ctor,添加可选参数,或映射所有构造函数参数 ==================================================================================================================自动映射器为您创建了此类型映射,但无法复制您的类型 使用当前配置映射。CountryModel->Country (目标成员列表)NetworkTool.Models.Location.CountryModel-> NetworkTool.Data.Country(目标成员列表)

未映射属性:城市


您正在映射的代码很好,如果一切都配置正确,应该可以正常工作。我相信您遇到的问题是,虽然您已经为您的示例正确配置了AM,但实际的映射程序正在执行从模型到实体的转换工作,而没有使用您的实际配置。像Eric一样,我运行了您提供的代码,它工作得很好……除非我在配置中注释掉实际的映射。我认为您出现此异常的原因是因为AM试图使用自己的约定映射而不是您定义的映射进行转换。AM可以自动计算出Id和名称,无需配置,但在城市中失败。您需要确保您的_映射器实际上指向“已配置映射器”

如果我运行这个(正如您所拥有的),代码工作正常

public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Country, CountryModel>();
            cfg.CreateMap<CountryModel, Country>().ForMember(x => x.City, opt => opt.Ignore());
        });
    }
publicstaticvoidconfigure()
{
Mapper.Initialize(cfg=>
{
CreateMap();
cfg.CreateMap().formMember(x=>x.City,opt=>opt.Ignore());
});
}
如果我运行这个(映射被注释掉),它将抛出未映射异常

public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Country, CountryModel>();
            //cfg.CreateMap<CountryModel, Country>().ForMember(x => x.City, opt => opt.Ignore());
        });
    }
publicstaticvoidconfigure()
{
Mapper.Initialize(cfg=>
{
CreateMap();
//cfg.CreateMap().formMember(x=>x.City,opt=>opt.Ignore());
});
}
如下所示

在默认注册表中,我有以下内容:

    var config = new MapperConfiguration(cfg =>
    {
        foreach (var profile in profiles)
        {
            cfg.AddProfile(profile);
        }
    });

    //Create a mapper that will be used by the DI container
    var mapper = config.CreateMapper();

    //Register the DI interfaces with their implementation
    For<IMapperConfiguration>().Use(config);
    For<IMapper>().Use(mapper);
var config=new-MapperConfiguration(cfg=>
{
foreach(配置文件中的var配置文件)
{
cfg.AddProfile(profile);
}
});
//创建DI容器将使用的映射器
var mapper=config.CreateMapper();
//注册DI接口及其实现
For()。使用(config);
For()。使用(映射器);
这明显改变了我的自动映射设置

我不得不把它改成:

var mapper = AutoMapperConfiguration.Configure();

For<IMapper>().Use(mapper.CreateMapper());
var-mapper=AutoMapperConfiguration.Configure();
For().Use(mapper.CreateMapper());
并将我的初始映射更改为:

public class AutoMapperConfiguration
{
    public static MapperConfiguration Configure()
    {
        var mapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Country, CountryModel>();
                cfg.CreateMap<CountryModel, Country>().ForMember(x => x.City, opt => opt.Ignore());
            }
        );

        return mapper;
    }
}
公共类自动映射器配置
{
公共静态MapperConfiguration配置()
{
var mapper=新的MapperConfiguration(cfg=>
{
CreateMap();
cfg.CreateMap().formMember(x=>x.City,opt=>opt.Ignore());
}
);
返回映射器;
}
}

然后我的mappngs终于工作了

我运行了上面的代码,它成功地执行了。它到底抛出了什么错误?您是从CountryModel转换到Country还是从CountryModel转换到Country?您所显示的只是对象类和配置,而不是如何实际触发从一个类到另一个类的映射。请参阅编辑2@EricDamtoft参见编辑3@ThunD3eR看见可能_mapper与mapper.Initialize使用的singleton不同?