0

我创建了新项目 asp.net 2.1 api 用于测试 DB first 方法,我有用于测试“DBTest”的数据库,它包含“Patients”表、“Medications”和“Ailments”表,它们使用外键引用“Patients”表,

关系图

为了创建模型,我使用了这个命令: PM> Scaffold-DbContext "Server=.\SQLEXPRESS;Database=DBtest;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2 -Context "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; }
    }

我为 Patients 创建了一个控制器 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

我在浏览器的控制台中发现了这个错误

邮递员结果

在代码优先方法中一切正常,但在数据库优先方法中我尝试了很多次(将药水添加到脚手架命令.....)但没有任何帮助,

有人遇到过这个问题吗?

4

1 回答 1

0

尝试这个:

services.AddMvc()
  .AddJsonOptions(x => 
     x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
  .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
于 2019-11-28T03:08:33.083 回答