Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射:错误映射类型。映射类型:IEnumerable`1->;IEnumerable`1_C#_Entity Framework_Asp.net Web Api_Automapper - Fatal编程技术网

C# 自动映射:错误映射类型。映射类型:IEnumerable`1->;IEnumerable`1

C# 自动映射:错误映射类型。映射类型:IEnumerable`1->;IEnumerable`1,c#,entity-framework,asp.net-web-api,automapper,C#,Entity Framework,Asp.net Web Api,Automapper,我试图将模型映射到视图,但是当我试图显示所有元素时,我收到了上面的错误,因为Automapper无法识别我认为的IEnumerable。尝试将FixedAssets映射到FixedAssetView和FixedAssetView映射到FixedAssets时收到错误 以下是我试图映射的对象: 固定资产 public class FixedAssets : IEntityBase { public int ID { get; set; } public string name {

我试图将模型映射到视图,但是当我试图显示所有元素时,我收到了上面的错误,因为Automapper无法识别我认为的IEnumerable。尝试将FixedAssets映射到FixedAssetView和FixedAssetView映射到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>();
    }
}
公共类固定资产:IEntityBase
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection类别{get;set;}
公共字符串serialNo{get;set;}
公共字符串提供程序{get;set;
公共日期时间获取日期{get;set;}
公共日期时间保修结束{get;set;}
public int inventoryNo{get;set;}
公共字符串分配状态{get;set;}
公共字符串所有者{get;set;}
公共日期时间分配日期{get;set;}
公共字符串serviceStatus{get;set;}
公共字符串serviceResolution{get;set;}
公共固定资产()
{
this.category=new HashSet();
}
}
固定资产视图

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; }
}
公共类固定资产视图
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection类别{get;set;}
公共字符串serialNo{get;set;}
公共字符串提供程序{get;set;}
公共日期时间获取日期{get;set;}
公共日期时间保修结束{get;set;}
public int inventoryNo{get;set;}
公共字符串分配状态{get;set;}
公共字符串所有者{get;set;}
公共日期时间分配日期{get;set;}
公共字符串serviceStatus{get;set;}
公共字符串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>();
    }
}
公共类类别:IEntityBase
{
公共int ID{get;set;}
公共字符串categoryName{get;set;}
公共虚拟ICollection fixedasset{get;set;}
公共类别()
{
this.fixedasset=new HashSet();
}
}
分类视图

public class CategoryView
{
    public int ID { get; set; }
    public string categoryName { get; set; }

    public virtual ICollection<FixedAssetsView> fixedasset { get; set; }
}
公共类类别视图
{
公共int ID{get;set;}
公共字符串categoryName{get;set;}
公共虚拟ICollection fixedasset{get;set;}
}
自动映射配置

Mapper.Initialize(x =>
        {
            x.CreateMap<FixedAssets, FixedAssetsView>();
            x.CreateMap<FixedAssetsView, FixedAssets>();

            x.CreateMap<Category, CategoryView>();
            x.CreateMap<CategoryView, Category>();

        });
Mapper.Initialize(x=>
{
x、 CreateMap();
x、 CreateMap();
x、 CreateMap();
x、 CreateMap();
});

我相信在映射器初始化中需要.formmember

例如:

Mapper.CreateMap()
.ForMember(f=>f,mp=>mp.MapFrom(
mfrom=>mfrom.Select(s=>AutoMapper.Mapper.Map(s,newtarget())
)
);

我相信在映射器初始化中需要.formmember

例如:

Mapper.CreateMap()
.ForMember(f=>f,mp=>mp.MapFrom(
mfrom=>mfrom.Select(s=>AutoMapper.Mapper.Map(s,newtarget())
)
);

我看不到MyGet的最新版本有任何错误,但另一个答案可能适用。请参阅。此处缺少的可能是实际执行映射的代码。当映射出现问题时,会发生此错误。原因隐藏在仅显示错误映射类型的异常消息中。映射类型:IEnumerable1->IEnumerable1若要查找原因,请尝试使AutoMapper仅映射集合中的一项。如果出现异常,异常消息可能会更好地解释问题所在。我看不到MyGet的最新版本有任何错误,但可能会应用另一个答案。请参阅。此处缺少的内容可能重复实际执行映射的代码。当映射出现问题时会发生此错误。原因隐藏在异常消息中,该消息仅显示:错误映射类型。映射类型:IEnumerable1->IEnumerable1若要查找原因,请尝试使AutoMapper仅映射集合的一项。如果出现异常,则该消息可能包含对错误的更好解释。
Mapper.CreateMap<IEnumerable<Source>, IEnumerable<Target>>()
.ForMember(f => f, mp => mp.MapFrom(
                                mfrom => mfrom.Select(s => AutoMapper.Mapper.Map(s, new Target())
                            )
          );