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# 自动映射,将一种类型映射为两种不同类型_C#_Entity Framework_Entity Framework Core_Automapper - Fatal编程技术网

C# 自动映射,将一种类型映射为两种不同类型

C# 自动映射,将一种类型映射为两种不同类型,c#,entity-framework,entity-framework-core,automapper,C#,Entity Framework,Entity Framework Core,Automapper,我有以下型号: 公共类的东西 { ... 公共IList位置{get;set;}=null!; ... } 公共课场所 { ... 公共IList stuff{get;set;}=null!; ... } 公共类实体 { ... 公共IList位置{get;set;}=null!; ... } 公共类实体 { ... 公共IList stuff{get;set;}=null!; ... } 公共类PlaceStuffEntity { public int StuffId{get;private

我有以下型号:

公共类的东西
{
...
公共IList位置{get;set;}=null!;
...
}
公共课场所
{
...
公共IList stuff{get;set;}=null!;
...
}
公共类实体
{
...
公共IList位置{get;set;}=null!;
...
}
公共类实体
{
...
公共IList stuff{get;set;}=null!;
...
}
公共类PlaceStuffEntity
{
public int StuffId{get;private set;}
public StuffEntity Stuff{get;set;}=null!;
public int PlaceId{get;private set;}
public PlaceEntity Place{get;set;}=null!;
}
cfg.CreateMap()
.FormMember(d=>d.位置,
opt=>opt.MapFrom(s=>s.Places.Select(y=>y.Place.ToList());
cfg.CreateMap()
.FormMember(d=>d.Stuff,
opt=>opt.MapFrom(s=>s.Places.Select(y=>y.Stuff.ToList());
cfg.CreateMap()/<--问题
.IncludeMembers(entity=>entity.Stuff);
cfg.CreateMap()/<--问题
.IncludeMembers(entity=>entity.Place);
由于某种原因,当我添加最后两行时,转换不起作用

但是如果我只添加一行,例如用于转换
PlaceAndStuffEntity
->
Stuff
的代码,那么从
PlaceEntity
->
Place


var place=mapper.Map(placeEntity);// 听起来您想通过连接表(PlaceAndStuff)映射到另一个实体类型。例如,在您的位置中,要获取内容列表,而在Stuff中要获取位置列表,您需要指导Automapper如何在连接表中导航

例如:

cfg.CreateMap<StuffEntity, Stuff>()             
   .ForMember(x => x.Places, opt => opt.MapFrom(src => src.PlaceEntity));
// Where StuffEntity.Places = PlaceAndStuffEntities, to map Stuff.Places use PlaceAndStuffs.PlaceEntity

cfg.CreateMap<PlaceEntity, Place>()             
   .ForMember(x => x.Stuffs, opt => opt.MapFrom(src => src.StuffEntity));
cfg.CreateMap()
.ForMember(x=>x.Places,opt=>opt.MapFrom(src=>src.PlaceEntity));
//其中,StuffEntity.Places=PlaceAndStuffEntities,要映射Stuff.place,请使用PlaceAndStuffs.PlaceEntity
cfg.CreateMap()
.ForMember(x=>x.Stuffs,opt=>opt.MapFrom(src=>src.StuffEntity));
因此,我们将重点放在PlaceEntity和StuffEntity上,而不是试图告诉EF如何映射加入实体PlaceStuffEntity,并告诉Automapper在加入实体中导航,通过加入实体获取实际的内容和位置亲属。

更改

cfg.CreateMap<PlaceEntity, Place>()
   .ForMember(d => d.Stuffs,
              opt => opt.MapFrom(s => s.Places.Select(y => y.Stuff).ToList()));
cfg.CreateMap()
.FormMember(d=>d.Stuff,
opt=>opt.MapFrom(s=>s.Places.Select(y=>y.Stuff.ToList());

cfg.CreateMap()
.FormMember(d=>d.Stuff,
opt=>opt.MapFrom(s=>s.Stuff.Select(y=>y.Stuff.ToList());
源类型PlaceEntity没有名为Places的属性,只有Stuff

cfg.CreateMap<PlaceEntity, Place>()
   .ForMember(d => d.Stuffs,
              opt => opt.MapFrom(s => s.Stuffs.Select(y => y.Stuff).ToList()));