繁体   English   中英

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

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

我有以下两个由实体框架生成的类:

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; }
}

然后我的业务层中也有这两个类似的类:

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; }        
}

几乎一样,嗯? 在我的业务层中,我从数据库中读取了房屋列表。 然后我使用 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;
    }

但是,在以下行中,我收到运行时异常:

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

“错误映射类型”。

我猜,这可能是,因为每个 Person 都指向一个 House,每个 House 都指向 Person。 因为我的 BusinessLayer 中不需要这个“圆圈”,所以我用 [IgnoreMap] 装饰了这个属性,但没有任何成功。 错误仍然存​​在。

任何建议我做错了什么?

所以这最终解决了我的问题:

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

是的,错误仍然存​​在,没有ignoremap。 内部异常告诉我以下内容:

{"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]]"}

所以 House-Type 是问题所在。 我还尝试添加另一张地图:

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

没有成功和同样的错误。 :-(

尝试使用

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

你有循环引用 Person->House->ICollection

循环引用

以前,AutoMapper 可以通过跟踪映射的内容来处理循环引用,并且在每次映射时,检查源/目标对象的本地哈希表以查看项目是否已被映射。 事实证明,这种跟踪非常昂贵,您需要选择使用 PreserveReferences 才能使圆形地图工作。 或者,您可以配置 MaxDepth:

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

AutoMapper 文档

暂无
暂无

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

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