Linq 使用Mapster调整导航属性
我有一个Linq 使用Mapster调整导航属性,linq,entity-framework-core,mapster,Linq,Entity Framework Core,Mapster,我有一个Post类,其中包含User属性。当我尝试获取所有帖子时,我还希望将User映射到UserDto对象 public class Post { public Guid Id {get; set;} public string Content {get;set;} public User User {get; set;} } var result = await _ctx.Posts.Include(u => u.User.Adapt<UserDto&
Post
类,其中包含User
属性。当我尝试获取所有帖子时,我还希望将User
映射到UserDto
对象
public class Post {
public Guid Id {get; set;}
public string Content {get;set;}
public User User {get; set;}
}
var result = await _ctx.Posts.Include(u => u.User.Adapt<UserDto>()).ToListAsync()
公共类职位{
公共Guid Id{get;set;}
公共字符串内容{get;set;}
公共用户{get;set;}
}
var result=wait _ctx.Posts.Include(u=>u.User.Adapt()).ToListAsync()
在Include
中进行调整会引发以下错误:
包含中使用的Lambda表达式无效
您似乎混淆了
Include
,因为entityframework
和Mapster
都具有该功能。您向我们展示的包含
属于实体框架
:
因此,首先需要使用Include
检索数据,如下所示:
var result = await _ctx.Posts.Include(u => u.User).ToListAsync();
另一方面,您需要设置mapster配置:
TypeAdapterConfig<Post, PostDto>.NewConfig()
.PreserveReference(true);
TypeAdapterConfig<User, UserDto>.NewConfig()
.PreserveReference(true);
您似乎混淆了
Include
,因为entityframework
和Mapster
都具有该功能。您向我们展示的包含
属于实体框架
:
因此,首先需要使用Include
检索数据,如下所示:
var result = await _ctx.Posts.Include(u => u.User).ToListAsync();
另一方面,您需要设置mapster配置:
TypeAdapterConfig<Post, PostDto>.NewConfig()
.PreserveReference(true);
TypeAdapterConfig<User, UserDto>.NewConfig()
.PreserveReference(true);
如果没有mapster配置,只要Dto属性名与源对象名匹配,就可以正常工作。如果没有mapster配置,只要Dto属性名与源对象名匹配,就可以正常工作。这是预期的吗?