Hej,
problem dotyczy dokumentu zamówienia i jego pozycji
przykład
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
public int TargetCustomerId { get; set; }
public virtual Customer TargetCustomer { get; set; }
public int AddressId { get; set; }
public virtual Address Address { get; set; }
public DateTime RealizationDate { get; set; }
public DateTime DueDate { get; set; }
public string? Comments { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual List<OrderPosition> Positions { get; set; }
}
public class OrderPosition
{
public int Id { get; set; }
public int SkuId { get; set; }
public virtual Sku Sku { get; set; }
[Column(TypeName = "decimal(11,4)")]
public decimal quantity { get; set; }
public int OrderId { get; set; }
public virtual Order Order { get; set; }
}
próba wywołania
var order = _dbContext.Orders.Where(c => c.Id == id)
.Include(user => user.User)
.Include(address => address.Address)
.Include(customer => customer.Customer)
.Include(targetCustomer => targetCustomer.TargetCustomer)
.Include(lines => lines.Positions)
.ThenInclude(sku => sku.Sku)
.FirstOrDefault();
daje exception possible object cycle was detected. W debugu kręcę się w kółko o ordera po pozycje i z powrotem.
wygooglałem taką konfigurację aby to ignorować
builder.Services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles
);
ale czy to jest rozwiązanie?
co robię nie tak? wydaję się standardowe złączenie jeden do wielu ...
robiąc same pozycje
_dbContext.OrderPositions.Where(c => c.OrderId == 1).Include(sku => sku.Sku);
mogę dostać się do nagłówka zamówienia, nie do końca potrzebne
czy pozbyć się
public virtual Order Order { get; set; }
z postions? nie wiem jak przy zapisie potem....