2016-07-08 182 views
0

我有以下兩類由實體框架生成:使用Automapper到實體框架類映射到業務類

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我家業務類的列表:

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

「錯誤映射類型」。

我想,這可能是因爲每個人指向一個房子,每個房子指向人。因爲我在BusinessLayer中不需要這個「圓圈」,所以我用[IgnoreMap]裝飾了這個屬性,但沒有任何成功。錯誤仍然存​​在。

任何暗示我做錯了什麼?

+0

如果刪除'IgnoreMap'屬性是它的工作 – Venky

+1

'錯誤映射Types'錯誤也給什麼類型不映射,你可以得到的。?。進入內部的異常細節並粘貼到這裏 – Venky

+1

此外,您需要爲'Person'實體顯式創建'mapper',因爲它們在'House'實體中被引用。 – Venky

回答

0

因此,這最終會解決我的問題:

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

是的,錯誤仍然沒有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>()); 

沒有成功和相同的錯誤。 :-(

相關問題