Includes in Entity Framework Core

Asked

Viewed 56 times

1

Good afternoon,

I have a problem to use Entity Framework Core, when I will do a query with includes as below:

var protocolos = _protocoloService.Search(p => p.Active == true,
       p => p.Include(l => l.Local)
       , tracking: false).ToList();

The json returned by this code is approximately 80mb. I did a test using the following code:

 var protocolos = _protocoloService.Search(p => p.Active == true, tracking: false).ToList();

 foreach(Protocolo p in protocolos)
 {
       Local local = _localService.GetById(p.ID_Local).Result;
       p.Local = local;
 }

The returned json is only 500kb now. Note: I am using Asnotracking.

Does anyone have any suggestions as to what it might be? Thank you

  • It is probably circular dependency.

  • { opts.SerializerSettings.Formatting = Formatting.None; opts.SerializerSettings.Contractresolver = new Lowercasecontractresolver(); opts.SerializerSettings.Referenceloophandling = Referenceloophandling.Ignore; opts.SerializerSettings.Nullvaluehandling = Nullvaluehandling.Ignore; } This is the json settings I’m using

  • Is your Getbyid not asynchronous? You need to add the await, otherwise the API will respond before all queries are performed.

1 answer

0


Your second code uses Result, so you must be using a return Task. As it works asynchronously, your API is returning before all queries and the object’s Binds are completed. Try to add the AWAIT operator so that the thread is blocked and can return the JSON correctly on callback:

 var protocolos = _protocoloService.Search(p => p.Active == true, tracking: false).ToList();

 foreach(Protocolo p in protocolos)
 {
       Local local = await _localService.GetById(p.ID_Local);
       p.Local = local;
 }

The first code works because Tolist() is not asynchronous and it is on the scope of Include.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.