我创建了用于测试数据库优先方法的新项目asp.net 2.1 api,我具有用于测试“ DBTest”的数据库,其中包含“患者”表,“药物”和“疾病”表,其中引用了带有外键的“患者”表,
为了创建模型,我使用了以下命令: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();
}
我没有回应,
我在浏览器的控制台中发现此错误
在代码中先解决所有问题,但在数据库中先解决我尝试了很多次(在scaffold命令中添加药水.....),但没有任何帮助,
有人遇到这个问题了吗?
尝试一下:
services.AddMvc()
.AddJsonOptions(x =>
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);