无法从EF(到EF)进行自动映射



所以我早些时候想制作一个自己的自定义映射器,但我得到的建议是这很困难(因为我有refracts等,我同意^^),而且我会在自动映射器方面运气更好,因为它很容易等等。因此,我开始为我想要转换的类创建映射(我想让这个应用程序每周从SQLite文件更新一个SQLserver数据库,表名和属性是相同的,只有大写字母不相同,但automapper不区分大小写,所以很棒)

这是我尝试过的代码:

Mapper.CreateMap<person, Person>();
Mapper.CreateMap<personAbility, PersonAbility>();
Mapper.AssertConfigurationIsValid();

然后我得到了一个错误,那个人不知道如何转换人格属性。所以我在谷歌上搜索了一下,发现我需要包括它:

Mapper.CreateMap<person, Person>()
.Include<personAbility, PersonAbility>()
Mapper.CreateMap<personAbility, PersonAbility>();
Mapper.AssertConfigurationIsValid();

然而,它在这里抱怨说,它需要的不是人物角色,而是一个列表(EntityCollection)。所以我尝试了:

Mapper.CreateMap<person, Person>()
.Include<EntityCollection<personAbility>, EntityCollection<PersonAbility>>()
Mapper.CreateMap<personAbility, PersonAbility>();
Mapper.AssertConfigurationIsValid();

然而,它给出了一个不知道类型的错误,我在谷歌上搜索了更多的发现:https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays支持列表上没有EntityCollection。意思是我撞到了另一面墙(死胡同)???任何人都知道这方面的解决方案,如果你有其他解决方案/方法来映射我的数据库,我们将不胜感激。

我不是肯定的,但在为Person创建映射之前,您可能需要为PersonAbility创建映射,因为映射Person的映射取决于是否能够映射PersonAbility的

我最终修复了它。我和include走错了路。我只需要映射集合太

添加此行修复了它:(并删除包含行)

        Mapper.CreateMap<EntityCollection<personAbility>, EntityCollection<PersonAbility>>();

感谢所有的努力

最新更新