Я пытаюсь изучить C # Fluent API, и у меня возникают проблемы (я думаю) с настройкой модели. У меня есть три таблицы: OrderFile, Order, LineItem. Ошибка:
Обнаружен самоссылочный цикл для свойства order с типом BaseService.WebApi.Order. Путь 'orders [0] .lineItems [0]'.
Моя структура:
- OrderFile содержит
List<Orders>
- Заказ содержит
List<ListItems>
и свойство навигации OrderFile
- ListItem содержит свойство Navigation
Order
Они связаны с ограничениями ForeignKey, указанными в Fluent API. Что-то не так с ограничениями? Я пытался следовать этому примеру для внешних ключей
modelBuilder.Entity<OrderFile>(e =>
{
//many orders within one order file
//the FK relates the OrderFile to the nav key of the Order
e.HasMany(of => of.Orders)
.WithOne(o => o.orderFile)
.HasForeignKey(o => o.FileGuid);
e.HasKey(o => o.FileGuid);
});
modelBuilder.Entity<Order>(e =>
{
//each order has an array of line items
//each line item has one order (navigation property)
//the foreign key of the line item ties it to the Parent (List<Order>)
e.HasMany(o => o.LineItems)
.WithOne(li => li.order)
.HasForeignKey(o => o.OrderGuid);
e.HasKey(o => o.OrderGuid);
});
Модель
public class OrderFile
{
public Guid FileGuid { get; set; }
public virtual ICollection<Order> Orders { get; set; } //everything with same FileGuid
}
public class Order
{
....
[JsonIgnore]
public Guid FileGuid { get; set; }
[Key]
public Guid OrderGuid { get; set; }
[JsonIgnore]
public OrderFile orderFile { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; } //everything with same OrderGuid
}
public class LineItem
{
....
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public Guid OrderGuid { get; set; }
public Order order { get; set; }
}