asp.net core 2.1,include()功能不适用于db first appro

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

asp.net core 2.1,include()功能不适用于db first appro

我使用以下命令创建模型: 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();
        } 

我没有回应,

asp.net core 2.1,include()功能不适用于db first appro

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

asp.net core 2.1,include()功能不适用于db first appro

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

有人遇到过这个问题吗?

dongdong28098 回答:asp.net core 2.1,include()功能不适用于db first appro

尝试一下:

services.AddMvc()
  .AddJsonOptions(x => 
     x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
  .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
本文链接:https://www.f2er.com/3018736.html

大家都在问