asp.net core 2.1,Include()函数不适用于数据库优先方法

问题描述 投票:0回答:1

我创建了用于测试数据库优先方法的新项目asp.net 2.1 api,我具有用于测试“ DBTest”的数据库,其中包含“患者”表,“药物”和“疾病”表,其中引用了带有外键的“患者”表,

diagrams relational

为了创建模型,我使用了以下命令:PM> Scaffold-DbContext“ Server =。\ SQLEXPRESS; Database = DBtest; Trusted_Connection = True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2-上下文“ DataContext2”

模型和DBcontext已成功生成,

public partial class Patients
    {
        public Patients()
        {
            Ailments = new HashSet<Ailments>();
            Medications = new HashSet<Medications>();
        }

        public int PatientId { get; set; }
        public string Name { get; set; }

        public ICollection<Ailments> Ailments { get; set; }
        public ICollection<Medications> Medications { get; set; }
    }

[我为患者创建了一个控制器api,GET方法可以正确响应(我用Postman测试),但是当我使用Include()函数时,例如:

// GET: api/Patients2
        [HttpGet]
        public IEnumerable<Patients> GetPatients()
        {
            return _context.Patients.Include(a => a.Ailments).ToList();
        } 

我没有回应,

ERR_HTTP2_PROTOCOL_ERROR 200

我在浏览器的控制台中发现此错误

postman result

在代码中先解决所有问题,但在数据库中先解决我尝试了很多次(在scaffold命令中添加药水.....),但没有任何帮助,

有人遇到这个问题了吗?

asp.net-core include dbcontext asp.net-core-2.1 db-first
1个回答
0
投票

尝试一下:

services.AddMvc()
  .AddJsonOptions(x => 
     x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
  .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
© www.soinside.com 2019 - 2024. All rights reserved.