实体框架核心禁用.include()函数中的信息的递归检索

编程入门 行业动态 更新时间:2024-10-26 03:37:41
本文介绍了实体框架核心禁用.include()函数中的信息的递归检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

该数据库具有机械",车轮",特性"和飞行员"表格.汽车的1条记录包括1条关于飞行员的记录,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网站说,如果删除虚拟关键字,则可以禁用下载.我打扫了一下,但没有帮助.同样在上下文中,我规定了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 core 2.x,您可以在Startup.cs中使用以下代码:

For asp 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 core 3.x,您需要先安装Microsoft.AspNetCore.Mvc.NewtonsoftJson,然后使用以下代码:

For asp 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; });

更多推荐

实体框架核心禁用.include()函数中的信息的递归检索

本文发布于:2023-11-15 12:30:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   实体   函数   框架   核心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!