Entity Framework Core 禁用 .Include() 函数中信息的递归检索 [英] Entity Framework Core disable recursive retrieval of information in .Include() function

查看:21
本文介绍了Entity Framework Core 禁用 .Include() 函数中信息的递归检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该数据库包含表 Machines、Wheels、Characteristics 和 Pilot.汽车的1条记录包括1条关于Pilot的记录,4条关于车轮的记录以及若干特性.在我的 Models 类中,它看起来像这样:

The database has tables Machines, Wheels, Characteristics and Pilot. 1 record of the car includes 1 records about Pilot, 4 records of a wheel and several characteristics. In my Models class, it looks something like this:

public class Machines
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Color { get; set; }
    public int Weight { get; set; }
    public int Param { get; set; }

    public List<Characteristics> characteristics { get; set; }
    public List<Wheels> wheels { get; set; }
    public Pilot pilot { get; set; }
}
public class Characteristics
{
    public int Id { get; set; }
    public string Parameter { get; set; }
    public string Value { get; set; }
    public string Description { get; set; }

    public int MachineId { get; set; }
    [ForeignKey("MachineId")]
    public Machines machine{ get; set; }

}
public class Wheels
{
    public int Id { get; set; }
    public int Radius { get; set; }
    public int Weight { get; set; }
    public int MachineId { get; set; }

    [ForeignKey("MachineId")]
    public Machines machine{ get; set; }
}
public class Pilot
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; } 
    public int MachineId { get; set; }

    [ForeignKey("MachineId")]
    public Machines machine{ get; set; }
}

当我尝试下载有关这样一台机器的完整信息时:

When I try to download full information about a machine like this:

 var car  = context.Machines.Where(x => x.Id == 2)
                        .Include(m=> m.Wheels)
                        .Include(m=>m.Charateristics)
                        .Include(m=>m.Pilot)
                        .FirstOrDefault();

作为回应,我得到了一辆包含飞行员、所有特征数组和车轮数组的汽车.但同时,轮子数组再次包含有关汽车的信息,其中包括有关轮子的信息(但这是第二次没有汽车).

In response, I get a car that contains a pilot, an array of all the characteristics, and an array of wheels. But at the same time, the array of wheels contains again information about the car, which includes information about the wheels (but for the second time without a car).

看起来像这样:

{
"id": 2,
"Description": "",
"Color": "red",
"Weight": 2000,
"Pilot": {
    "id": 1,
    "Name": "John",
    "Description": ""
}, 
"Wheels": [
    {
        "id": 7,
        "Radius": 14,
        "Weight": 5,
        "MachineId": 2, 
        "machine": {
            "id": 2,
            "Description": "",
            "Color": "red",
            "Weight": 2000,
            "Pilot": {
                "id": 1,
                "Name": "John",
                "Description": ""
            }, 
            "Wheels": [
                {
                    "id": 7,
                    "Radius": 14,
                    "Weight": 5,
                    "MachineId": 2
                },
...

如何在没有递归数据的情况下获取信息?

How do I get information without recursive data?

此外,请求需要很长时间(比 4 个单独的请求长得多).Microsoft 网站 说,如果您删除 virtual 关键字,则可以禁用下载.我清理过,但没有帮助.同样在上下文中,我规定了 this.ChangeTracker.LazyLoadingEnabled = false; 但这也没有帮助.

Also, the request takes a very long time (much longer than 4 separate requests). The Microsoft website says that you can disable the download if you remove the virtual keyword. I cleaned, but it did not help. Also in the context, I prescribed this.ChangeTracker.LazyLoadingEnabled = false; But this did not help either.

我也看到很多地方提到使用 .Load() 函数可以加快请求的执行,但我不太明白如何在我的情况下使用它.

I also saw a lot where the use of the .Load() function is mentioned which can speed up the execution of the request, but I did not quite understand how to use it in my case.

更新

由于每个人都建议我包含 Newtonsoft.Json.ReferenceLoopHandling.Ignore,我将提供我的启动和上下文文件.事实上,我已经启用了这个.在我添加这个之前,我根本无法发送响应,在我添加它之后,它开始按照我的指示来(参见上面的 JSON 响应).

Since everyone advises me to include Newtonsoft.Json.ReferenceLoopHandling.Ignore I will give my Startup and context files. In fact, I already have this enabled. Until I added this, I was not able to send a response at all, and after I added it, it began to come as I indicated (See JSON responce upper).

我的背景:

    public DbSet<Machines> machines{ get; set; }
    public DbSet<Characteristics> characteristics{ get; set; }
    public DbSet<Wheels> wheels{ get; set; }
    public DbSet<Pilot> pilot{ get; set; }

    public dbContext(DbContextOptions<dbContext> options) : base(options)
    {
        this.ChangeTracker.LazyLoadingEnabled = false;
    }

我的启动:

public void ConfigureServices(IServiceCollection services)
{
   ...

   services.AddDbContext<dbContext>(options => { 

        options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), 
        builder =>
        {
            builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
        });
        }); 

        services.AddControllers().AddNewtonsoftJson(x =>
 x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

}

推荐答案

对于 asp.net core 2.x,你可以在 Startup.cs 中使用以下代码:

For asp.net core 2.x,you could use the following code in your Startup.cs:

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

对于asp.net core 3.x,您需要先安装Microsoft.AspNetCore.Mvc.NewtonsoftJson,然后使用以下代码:

For asp.net core 3.x,you need first install Microsoft.AspNetCore.Mvc.NewtonsoftJson then use the following code:

services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

这篇关于Entity Framework Core 禁用 .Include() 函数中信息的递归检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆