I create new project asp.net 2.1 api for testing DB first approach , I have database for testing "DBTest" it contains “Patients” table, ”Medications” and ”Ailments” tables witch references “Patients” table with foreign keys ,
for creating models I used this command : PM> Scaffold-DbContext "Server=.\SQLEXPRESS;Database=DBtest;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2 -Context "DataContext2"
The models and DBcontext were generated successfully,
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; }
}
I created a controller api for Patients , GET methods gives responses correctly (I test with Postman), but when I use Include() function, for example:
// GET: api/Patients2
[HttpGet]
public IEnumerable<Patients> GetPatients()
{
return _context.Patients.Include(a => a.Ailments).ToList();
}
I had no response ,
I found this error in browser's console
in code first approch everything is OK but in db first approche I tried many time ( add potions to scaffold command..... ) but nothing help,
Did someone encountered this probleme ?
Try this:
services.AddMvc()
.AddJsonOptions(x =>
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);