【问题标题】:EF Core one-to-many infinite includesEF Core 一对多无限包括
【发布时间】:2018-12-14 13:05:51
【问题描述】:

我有两个表 Appointment 和 TaskAllocation 具有一对多的关系。现在当我得到约会时

public IEnumerable<Appointment> GetAppointments(int employeeId, DateTime date)
    {
        return _context.Appointment.Where(a => a.EmployeeId == employeeId && 
            a.AppointmentDate == date)
           .Include(a=>a.Tasks).ToList();
    }

它会导致包括一项与许多任务的约会,以及一项与该约会与许多任务的约会,依此类推。

【问题讨论】:

  • 我遇到了同样的问题,通过查看输出我注意到在初始调用获取数据后没有调用加载数据。

标签: entity-framework .net-core ef-fluent-api


【解决方案1】:

在你的 ConfigureService 中,你需要添加 Json Options 来处理引用循环处理

.AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
    options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
});

或者你可以选择直接忽略引用循环

options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2018-02-02
    • 2018-05-06
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多