[英]AutoMapper with a complex EF Entity
I have a basic table with a few FK references. 我有一些FK参考的基本表。 So when I retrieve an entity for an update operation; 因此,当我检索实体进行更新操作时; that entity contains ICollections
of related entites. 该实体包含相关实体的ICollections
。 I also have a main ViewModel inside which each entity is a sub viewModel. 我也有一个主ViewModel,其中每个实体都是一个子viewModel。 My aim is to use Automapper like: 我的目标是像这样使用Automapper:
mapper.Map(MainViewmodel obj,MainEntity obj);
For this I have a MappingConfiguration like: 为此,我有一个MappingConfiguration像:
CreateMap<MainViewModel, MainEntity>();
CreateMap<subViewModel1, subEntity1>();
CreateMap<subViewModel2, subEntity2>();
This gives me an Unmapped properties exception because the ICollections
are not getting mapped. 这给了我一个Unmapped属性异常,因为未映射ICollections
。 Could anyone please guide me on the best way to deal with this kind of situation? 有人可以指导我解决这种情况的最佳方法吗?
thanks. 谢谢。
If I understand you correctly, you has classes like this: 如果我对您的理解正确,那么您将拥有如下课程:
class MainViewModel
{
ICollection<SubViewModel1> SubViewModels { get;set; }
}
class SubViewModel1
{
}
and 和
class MainEntity
{
ICollection<SubEntity1> SubEntities { get;set; }
}
class SubEntity1
{
}
Then you should create rules for each class, and collection of such classes automaper map automatically. 然后,您应该为每个类创建规则,并自动为此类分类的集合自动映射。
CreateMap<MainViewModel, MainEntity>();
CreateMap<SubViewModel1, SubEntity1>();
Addditions 1: 加法1:
var mappedMainEntity = rmapper.Map<MainEntity>(MainViewmodel obj);
尝试以下方法: var mappedMainEntity = rmapper.Map<MainEntity>(MainViewmodel obj);
If you map MainEntity
to MainViewModel
you need add .ReverseMap() to map rule, like this: 如果将MainEntity
映射到MainViewModel
,则需要添加MainEntity
()来映射规则,如下所示:
CreateMap().ReverseMap(); CreateMap()。ReverseMap(); CreateMap().ReverseMap(); CreateMap()。ReverseMap();
Additions 2: 补充2:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.