gpt4 book ai didi

c# - System.InvalidOperationException' 出现在 AutoMapper.dll 中。附加信息 : Mapper not initialized

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:42 24 4
gpt4 key购买 nike

我发现 AutoMapper 使用起来非常简单。我正在为新版本而苦苦挣扎。我有两种类型:

namespace VehicleMVC.Models
{
public class CarModel
{
public int id { get; set; }
public string make { get; set; }
public string model { get; set; }

}
}

和:

namespace Business
{
public class Car
{

private int _id;
private string _make;
private string _model;

public int id
{
get { return _id; }
set { _id = value; }
}

public string make
{
get { return _make; }
set { _make = value; }
}

public string model
{
get { return _model; }
set { _model = value; }
}

}
}

我已经在 CarController 中试过了:

public CarController()
{
service = new Service.Service();
//Mapper.Initialize(cfg => cfg.CreateMap<Business.Car,CarModel>());
//Mapper.Initialize(cfg => cfg.CreateMap<List<CarModel>, List<Business.Car>>());
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Business.Car, CarModel>();
} );
config.AssertConfigurationIsValid();

}

private CarModel getCarModel(Business.Car BusinessCar)
{
CarModel CarModel = AutoMapper.Mapper.Map<CarModel>(BusinessCar);
return CarModel;
}

我得到的错误是:AutoMapper.dll 中发生了类型为 'System.InvalidOperationException' 的异常。附加信息:映射器未初始化。使用适当的配置调用 Initialize。 但未在用户代码中处理。怎么了?

最佳答案

一旦你创建了你的配置,你必须用它来初始化映射器:

var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Business.Car, CarModel>();
};

config.AssertConfigurationIsValid();
Mapper.Initialize(config);

关于c# - System.InvalidOperationException' 出现在 AutoMapper.dll 中。附加信息 : Mapper not initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com