简体   繁体   中英

asp.net core 2.1, Include() fonction doesn't work with db first approch

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 ,

ERR_HTTP2_PROTOCOL_ERROR 200

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM