【发布时间】:2018-11-28 20:33:48
【问题描述】:
我正在尝试将模型映射到视图,但是当我尝试显示所有元素时收到上述错误,因为 Automapper 无法识别我认为的 IEnumerable。当我尝试将 FixedAssets 映射到 FixedAssetsView 并将 FixedAssetsView 映射到 FixedAssets 时收到错误消息。
这是我要映射的对象:
固定资产
public class FixedAssets : IEntityBase
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<Category> category { get; set; }
public string serialNo { get; set; }
public string provider { get; set;
public DateTime acquisitionDate { get; set; }
public DateTime warrantyEnd { get; set; }
public int inventoryNo { get; set; }
public string allocationStatus { get; set; }
public string owner { get; set; }
public DateTime allocationDate { get; set; }
public string serviceStatus { get; set; }
public string serviceResolution { get; set; }
public FixedAssets()
{
this.category = new HashSet<Category>();
}
}
固定资产视图
public class FixedAssetsView
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<CategoryView> category { get; set; }
public string serialNo { get; set; }
public string provider { get; set; }
public DateTime acquisitionDate { get; set; }
public DateTime warrantyEnd { get; set; }
public int inventoryNo { get; set; }
public string allocationStatus { get; set; }
public string owner { get; set; }
public DateTime allocationDate { get; set; }
public string serviceStatus { get; set; }
public string serviceResolution { get; set; }
}
类别
public class Category : IEntityBase
{
public int ID { get; set; }
public string categoryName { get; set; }
public virtual ICollection<FixedAssets> fixedasset { get; set; }
public Category()
{
this.fixedasset = new HashSet<FixedAssets>();
}
}
分类视图
public class CategoryView
{
public int ID { get; set; }
public string categoryName { get; set; }
public virtual ICollection<FixedAssetsView> fixedasset { get; set; }
}
自动映射器配置
Mapper.Initialize(x =>
{
x.CreateMap<FixedAssets, FixedAssetsView>();
x.CreateMap<FixedAssetsView, FixedAssets>();
x.CreateMap<Category, CategoryView>();
x.CreateMap<CategoryView, Category>();
});
【问题讨论】:
-
我没有看到来自 MyGet 的最新版本有任何错误,但另一个答案可能适用。见here。
-
这里缺少的是实际执行映射的代码。
-
当您的映射出现问题时会发生此错误。原因隐藏在仅显示的异常消息中:错误映射类型。映射类型:IEnumerable1 -> IEnumerable1 要查找原因,请尝试使 AutoMapper 仅映射集合中的一项。如果您遇到异常,异常消息可能会更好地解释问题所在。
标签: c# entity-framework asp.net-web-api automapper