簡體   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