【发布时间】:2016-07-08 14:05:57
【问题描述】:
我有以下两个实体框架生成的类:
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);
“错误映射类型”。
我想,这可能是因为每个人都指向一个房子,每个房子都指向一个人。因为我的 BusinessLayer 中不需要这个“圆圈”,所以我用 [IgnoreMap] 修饰了这个属性,但没有任何成功。错误仍然存在。
任何建议我做错了什么?
【问题讨论】:
-
如果您删除
IgnoreMap属性。有效吗? -
Error Mapping Types错误还给出了未映射的类型。你能进入内部异常细节并粘贴到这里吗? -
另外,您需要为
Person实体显式创建mapper,因为它们在House实体中被引用。
标签: c# asp.net .net entity-framework automapper