2

我正在执行下面的代码,EFCore 抛出

类型的表达式System.Nullable'1[System.Int32]不能用于类型的构造函数参数System.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 属性是可选的。

4

1 回答 1

1

另一种解决方法是在 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();
}
于 2018-12-06T04:57:36.383 回答