简体   繁体   English

使用 Automapper 将实体框架类映射到业务类

[英]Using Automapper to map entity framework classes to business classes

I have the following two classes generated by Entity Framework:我有以下两个由实体框架生成的类:

public partial class Person
{
    public int id { get; set; }
    public string namen { get; set; }
    public int house { get; set; }
    [IgnoreMap]
    public virtual House House1 { get; set; }
}

public partial class House
{
    public House()
    {
        this.Persons = new HashSet<Person>();
    }
    public int id { get; set; }
    public string street { get; set; }
    public string city { get; set; }
    public ICollection<Person> Persons { get; set; }
}

Then I also have these two similar classes in my Business Layer:然后我的业务层中也有这两个类似的类:

public class House
{        
    public House()
    {
        this.Persons = new HashSet<Person>();
    }
    public int id { get; set; }
    public string street { get; set; }
    public string city { get; set; }        
    public virtual ICollection<Person> Persons { get; set; }
}

public class Person
{
    public int id { get; set; }
    public string namen { get; set; }
    public int house { get; set; }        
}

Pretty much the same, hugh?几乎一样,嗯? In my Business Layer I read a List of houses from the Database.在我的业务层中,我从数据库中读取了房屋列表。 Then I map the whole list to a list of my Business house class using Automapper:然后我使用 Automapper 将整个列表映射到我的 Business house 类的列表:

    public List<elci.BusinessEntities.House> getHouses()
    {
        YardEntities cx = new YardEntities();
        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.House, BusinessEntities.House>());

        List<DataAccessLayer.House> dhl = cx.Houses.ToList();
        List<BusinessEntities.House> bhl = Mapper.Map<List<DataAccessLayer.House>, List<BusinessEntities.House>>(dhl);
        return bhl;
    }

However, in the following line I get a runtime exception:但是,在以下行中,我收到运行时异常:

 Mapper.Map<List<DataAccessLayer.House>, List<BusinessEntities.House>>(dhl);

"Error mapping types". “错误映射类型”。

I guess, that this might be, because every Person points to a House and every House points to Persons.我猜,这可能是,因为每个 Person 都指向一个 House,每个 House 都指向 Person。 Because I do not need this "circle" in my BusinessLayer I decorated this attribute with [IgnoreMap], but without any success.因为我的 BusinessLayer 中不需要这个“圆圈”,所以我用 [IgnoreMap] 装饰了这个属性,但没有任何成功。 The error still remains.错误仍然存​​在。

Any suggest what I am doing wrong?任何建议我做错了什么?

So this eventually fixed my issue:所以这最终解决了我的问题:

        Mapper.Initialize(cfg => {
            cfg.CreateMap<List<House>, List<HouseViewModel>>();
            cfg.CreateMap<List<Person>, List<PersonViewModel>>();
        });

Yes, the error remains without ignoremap.是的,错误仍然存​​在,没有ignoremap。 The inner exception tells me the following:内部异常告诉我以下内容:

{"Error mapping types.\r\n\r\nMapping types:\r\nList`1 -> List`1\r\nSystem.Collections.Generic.List`1[[elci.DataAccessLayer.House, DataAccessLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List`1[[elci.BusinessEntities.House, BusinessEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}

So the House-Type is the problem.所以 House-Type 是问题所在。 I also tried to add another map:我还尝试添加另一张地图:

        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.House, BusinessEntities.House>());
        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.Person, BusinessEntities.Person>());

without success and the same error.没有成功和同样的错误。 :-( :-(

Try to use尝试使用

config.CreateMap<TSource,TDestination>().PreserveReferences()

You have circular references Person->House->ICollection你有循环引用 Person->House->ICollection

Circular references循环引用

Previously, AutoMapper could handle circular references by keeping track of what was mapped, and on every mapping, check a local hashtable of source/destination objects to see if the item was already mapped.以前,AutoMapper 可以通过跟踪映射的内容来处理循环引用,并且在每次映射时,检查源/目标对象的本地哈希表以查看项目是否已被映射。 It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work.事实证明,这种跟踪非常昂贵,您需要选择使用 PreserveReferences 才能使圆形地图工作。 Alternatively, you can configure MaxDepth:或者,您可以配置 MaxDepth:

// Self-referential mapping
cfg.CreateMap<Category, CategoryDto>().MaxDepth(3);
// Circular references between users and groups
cfg.CreateMap<User, UserDto>().PreserveReferences();

AutoMapper docs AutoMapper 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM