Linq 自动映射和EFCore连接
我必须加入我想加入的表,我可以只使用EFCore,但我不知道如何也使用automapper 如果我有两门课Linq 自动映射和EFCore连接,linq,entity-framework-core,automapper,Linq,Entity Framework Core,Automapper,我必须加入我想加入的表,我可以只使用EFCore,但我不知道如何也使用automapper 如果我有两门课 public class ItemVM { public int Id {get; set;} public int ItemName { get; set; } public int GroupId {get; set;} } public class GroupVM { public int Id {get; set;} public stri
public class ItemVM {
public int Id {get; set;}
public int ItemName { get; set; }
public int GroupId {get; set;}
}
public class GroupVM {
public int Id {get; set;}
public string GroupName {get; set;}
}
最后我想说:
public class ItemWithGroupVM {
public int Id {get; set;}
public string Name {get; set;}
public int GroupId {get; set;}
public string GroupName {get; set;}
}
因此,我们所有的db字段都有前缀,因此在我的映射配置文件中,我有:
RecognizePrefixes("it");
CreateMap<Item, ItemVm>();
RecognizePrefixes("gr");
CreateMap<Group, GroupVm>();
识别前缀(“it”);
CreateMap();
识别前缀(“gr”);
CreateMap();
然后我运行我的查询:
var items = await _dbContext.Items.ProjectTo<ItemsVM>(_mapper.ConfigurationProvider).ToListAsync();
var groups = await _dbContext.Groups.ProjectTo<GroupsVM>(_mapper.ConfigurationProvider).ToListAsync();
var items=wait _dbContext.items.ProjectTo(_mapper.ConfigurationProvider.toListSync();
var groups=await _dbContext.groups.ProjectTo(_mapper.ConfigurationProvider.toListSync();
然后我只是循环遍历项目列表,生成一个新的项目列表SwithGroupVM,并为最终输出设置组名字符串
我如何修改我的automapper配置文件和ef core查询来进行连接和映射而不必循环 在
Item
和ProjectTo
中添加一个FK属性Group
,直接从Item
添加到ItemWithGroupVM
。在Item
中添加一个FK属性Group
和ProjectTo
直接从Item
添加到ItemWithGroupVM
。