Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Include和Include时的EF核心参考循环_C#_Entity Framework_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 使用Include和Include时的EF核心参考循环

C# 使用Include和Include时的EF核心参考循环,c#,entity-framework,asp.net-core,entity-framework-core,C#,Entity Framework,Asp.net Core,Entity Framework Core,在我的应用程序中,用户可以请求更多信息。然后,我需要能够提取这些信息,但我还需要能够看到提出请求的用户已经进行了哪些预订 我已与EF Core进行了以下跟进: 型号: public class InfoRequest { public int InfoRequestId { get; set; } public int UserId { get; set; } public User User { get; set } } public class User {

在我的应用程序中,用户可以请求更多信息。然后,我需要能够提取这些信息,但我还需要能够看到提出请求的用户已经进行了哪些预订

我已与EF Core进行了以下跟进:

型号:

public class InfoRequest
{
    public int InfoRequestId { get; set; }
    public int UserId { get; set; }
    public User User { get; set }
}

public class User
{
    public int UserId { get; set; }
    public ICollection<Booking> { get; set; }
}

public class Booking
{
    public int BookingId { get; set; }
    public int UserId { get; set; }
    public User User { get; set }
}
modelBuilder.Entity<InfoRequest>(builder =>
{
    builder.ToTable("inforequests");
    builder.HasKey(x => x.InfoRequestId);
});

modelBuilder.Entity<User>(builder =>
{
    builder.ToTable("userdetails");
    builder.HasKey(x => x.UserId);

    builder
        .HasMany(x => x.Bookings)
        .WithOne(x => x.User)
        .HasForeignKey(x => x.UserId);
});

modelBuilder.Entity<Booking>(builder =>
{
    builder.ToTable("bookings");
    builder.HasKey(x => x.BookingId);
});
如果不使用
ToList()
将其全部拉入内存,然后手动删除循环,如何防止这种情况发生?我只是希望EF一开始不要创建循环

我想要的是:

InfoRequest
    -> User
        -> Booking

EF/EF-Core中使用急切加载时,无法停止代理创建。这是EF/EF core的默认行为,无法更改

但可能是(我没有尝试过)您可以在EF-Core>=2.1中停止为延迟加载创建代理

但是,当您将实体转换为
JSON
时,可以根据代理停止
自引用循环,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}

有关更多详细信息:

用简单的文字!在C#代码中无法防止这种情况。但是你在JSON转换过程中停止这个,我不会用JSON做任何事情。这是在一个MVC应用程序的后端。那么你不能停止它!这是EF和EF核心的默认行为,无法更改。我不确定,但可能显式加载可以实现所述的技巧。例如,由于2.1添加了延迟加载,可以使其更好(至少不会立即加载),但它们需要使用延迟加载代理,例如,与上面相同的文档。
InfoRequest
    -> User
        -> Booking
public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}