在 DTO class 的用例中,包含会产生 null
Include produces null in use case with DTO class
我有一个 linq 语句似乎工作正常并获得正确的数据:
[HttpGet]
public async Task<IActionResult> Get()
{
List<DiaryRecord> diaryRecords = await this.Context.DiaryRecords
.Include(d => d.Project)
.Include(e => e.Employees)
.ToListAsync();
return Ok(diaryRecords);
}
员工是
public virtual ICollection<Personell> Employees { get; set; }
我通过以下方式在客户端组件中请求此列表:
this.DiaryRecords = await this.HttpClient
.GetFromJsonAsync<IEnumerable<DiaryRecordModelDTO>>("api/Diary");
员工在哪里:
public ICollection<PersonellDTO> Employees { get; set; }
作为输出 this.DiaryRecords
包含所有需要的信息,除了此处的 Employees 为空。是因为 类 PersonellDTO
和 Personell
不同导致的错误吗?如何让它发挥作用?
.GetFromJsonAsync 总是非常棘手,从不使用接口反序列化 json。并且 json 不知道 classe 使用了 PersonellDTO 还是 Personell 来创建 http 响应。
在您的 DTO 界面中,这应该固定为具体 class
public List<PersonellDTO> Employees { get; set; }
而且我总是为 httpclient 使用这样的代码
var response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<DiaryRecordModelDTO>(stringData);
}
我有一个 linq 语句似乎工作正常并获得正确的数据:
[HttpGet]
public async Task<IActionResult> Get()
{
List<DiaryRecord> diaryRecords = await this.Context.DiaryRecords
.Include(d => d.Project)
.Include(e => e.Employees)
.ToListAsync();
return Ok(diaryRecords);
}
员工是
public virtual ICollection<Personell> Employees { get; set; }
我通过以下方式在客户端组件中请求此列表:
this.DiaryRecords = await this.HttpClient
.GetFromJsonAsync<IEnumerable<DiaryRecordModelDTO>>("api/Diary");
员工在哪里:
public ICollection<PersonellDTO> Employees { get; set; }
作为输出 this.DiaryRecords
包含所有需要的信息,除了此处的 Employees 为空。是因为 类 PersonellDTO
和 Personell
不同导致的错误吗?如何让它发挥作用?
.GetFromJsonAsync 总是非常棘手,从不使用接口反序列化 json。并且 json 不知道 classe 使用了 PersonellDTO 还是 Personell 来创建 http 响应。
在您的 DTO 界面中,这应该固定为具体 class
public List<PersonellDTO> Employees { get; set; }
而且我总是为 httpclient 使用这样的代码
var response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<DiaryRecordModelDTO>(stringData);
}