我正在执行下面的代码,EFCore 抛出错误
Expression of type
System.Nullable'1[System.Int32]
cannot be used for constructor parameter of typeSystem.Int32'\r\nParameter name: arguments[0]
var data= await _dbContext.Set<Person>().Select(person =>person.Profile != null ?
new ProfileDto(org.Profile.Id , org.Profile.Nickname) : null).ToListAsync();
Person 要么有个人资料,要么没有,因此 Person 的 Profile 属性是可选的。
最佳答案
另一种解决方法是在 ProfileDto 上创建一个静态方法,例如,
public class ProfileDto
{
public static ProfileDto CreateFromDb(int id, string nickname)
{
// this is a constuctor.
return new ProfileDto(id,nickname);
}
}
//然后做:
{
var data= await _dbContext.Set<Person>().Select(person =>person.Profile != null ?
ProfileDto.CreateFromDb(org.Profile.Id , org.Profile.Nickname) : null).ToListAsync();
}
关于c# - 表达式类型 'System.Nullable` 1[System.Int32 ]' cannot be used for constructor parameter of type ' System.Int32'\r\n参数名称 : arguments[0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644404/