ASP.net AutoMapper 缺少类型映射配置或不支持的映射
ASP.net AutoMapper Missing type map configuration or unsupported mapping
领域模型:
public class Test
{
public int Id { get; set; }
}
查看模型:
public class TestViewModel
{
public int Id { get; set; }
}
Global.asax:
AutoMapper.Initialize();
AutoMapper:
public static class AutoMapper
{
public static void Initialize()
{
CreateModelsToViewModels();
CreateViewModelsToModels();
}
private static void CreateModelsToViewModels()
{
Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());
Mapper.AssertConfigurationIsValid();
}
private static void CreateViewModelsToModels()
{
}
}
配置有效。
public ActionResult Index(string category)
{
Test t = _Context.test.First(x => x.Id == 1);
var test = Mapper.Map<Test, TestViewModel>(t); //error here
}
只有数据是 Id 为 1 的一行。即使是这个简单的测试也不断向我抛出错误 Missing type map configuration or unsupported mapping
。
可能是什么问题?
编辑:将 Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());
更改为 Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>());
,但仍然是同样的错误
原来我在创建这些测试 类 映射后忘记更新数据库(使用数据库迁移)。
对于遇到此问题的任何其他人,如果您使用数据库迁移,请确保在程序包管理器控制台中使用 update-database
,否则您会收到此令人困惑的错误:
AutoMapper Missing type map configuration or unsupported mapping.
领域模型:
public class Test
{
public int Id { get; set; }
}
查看模型:
public class TestViewModel
{
public int Id { get; set; }
}
Global.asax:
AutoMapper.Initialize();
AutoMapper:
public static class AutoMapper
{
public static void Initialize()
{
CreateModelsToViewModels();
CreateViewModelsToModels();
}
private static void CreateModelsToViewModels()
{
Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());
Mapper.AssertConfigurationIsValid();
}
private static void CreateViewModelsToModels()
{
}
}
配置有效。
public ActionResult Index(string category)
{
Test t = _Context.test.First(x => x.Id == 1);
var test = Mapper.Map<Test, TestViewModel>(t); //error here
}
只有数据是 Id 为 1 的一行。即使是这个简单的测试也不断向我抛出错误 Missing type map configuration or unsupported mapping
。
可能是什么问题?
编辑:将 Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());
更改为 Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>());
,但仍然是同样的错误
原来我在创建这些测试 类 映射后忘记更新数据库(使用数据库迁移)。
对于遇到此问题的任何其他人,如果您使用数据库迁移,请确保在程序包管理器控制台中使用 update-database
,否则您会收到此令人困惑的错误:
AutoMapper Missing type map configuration or unsupported mapping.