[JsonIgnore] 是 using System.Text.Json.Serialization; 而非 using Newtonsoft.Json; 否則會吃不到設定
JsonException: A possible object cycle was detected.
This can either be due to a cycle or if the object depth
is larger than the maximum allowed depth of 32.
//Get a IQueryable<T> by including sub collections
var queryable = await _orderRepository.WithDetailsAsync(x => x.Lines);
//Apply additional LINQ extension methods
var query = queryable.Where(x => x.Id == id);
//Execute the query and get the result
var order = await AsyncExecuter.FirstOrDefaultAsync(query);
沒意外了話,order 裡面會包含 Line 的集合資料
[Fact]
public async Task Should_Get_Details_From_A_Order()
{
var orderManager = GetRequiredService<orderManager>();
//Act
var result = await WithUnitOfWorkAsync(async () =>
await orderManager.WithDetailsAsync()};
//Assert
result.ShouldNotBeNull();
result.Lines.ShouldNotBeNull();
result.Lines.Count.ShouldBeGreaterThan(0);
}
多層關聯 [Include(A).ThenInclude(B)]
//Get a IQueryable<T> by including sub collections
var queryable = await _orderRepository.WithDetailsAsync(
x => x.Lines,
x => x.Lines.Providers);
//Apply additional LINQ extension methods
var query = queryable.Where(x => x.Id == id);
//Execute the query and get the result
var result = await AsyncExecuter.ToListAsync(query, cancellationToken);
複數關聯 [Include(A).Include(C)]
//Get a IQueryable<T> by including sub collections
var queryable = await _orderRepository.WithDetailsAsync(
x => x.Lines,
x => x.Details);
//Apply additional LINQ extension methods
var query = queryable.Where(x => x.Id == id);
//Execute the query and get the result
var result = await AsyncExecuter.ToListAsync(query, cancellationToken);