3

In my webapi2 application I used db first generation and I created a partial class of context to set both ProxyCreationEnabledand LazyLoadingEnabled to false.

But when I use db.ExameViaAereaOssea.Where(e => e.ConsultaId == model.ConsultaId).ToList() the relation property Consulta is loaded too.

That not happing when I use db.ExameViaAereaOssea.AsNoTracking().Where(e => e.ConsultaId == consultaId).ToList()

I don't want use AsNoTracking for every query

Here are my two classes

public partial class ExameViaAereaOssea { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public ExameViaAereaOssea() { } public int ExameViaAereaOsseaId { get; set; } public Nullable<int> ConsultaId { get; set; } public string VAOE125 { get; set; } public string VAOE250 { get; set; } public string VAOE500 { get; set; } public string VAOE750 { get; set; } public string VAOE1000 { get; set; } public string VAOE1500 { get; set; } public string VAOE2000 { get; set; } public string VAOE3000 { get; set; } public string VAOE4000 { get; set; } public string VAOE6000 { get; set; } public string VAOE8000 { get; set; } public string VOOE500 { get; set; } public string VOOE750 { get; set; } public string VOOE1000 { get; set; } public string VOOE2000 { get; set; } public string VOOE3000 { get; set; } public string VOOE4000 { get; set; } public string TipoAudiometria { get; set; } public virtual Consulta Consulta { get; set; } } public partial class Consulta { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Consulta() { this.ExameViaAereaOssea = new HashSet<ExameViaAereaOssea>(); } public int ConsultaId { get; set; } public DateTime? Data {get;set;} public Nullable<int> PacienteId { get; set; } public Nullable<int> TipoConsultaId { get; set; } public Nullable<int> PacienteDadosProfissionaisId { get; set; } public Nullable<int> ClienteId { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<ExameViaAereaOssea> ExameViaAereaOssea { get; set; } } 

And here is the usage.

public class RegraConsulta(){ public ExamesConsulta BuscaExamesConsulta(Consulta model) { using (var db = new winmedEntities(false)) { var retorno = new ExamesConsulta(); retorno.DataConsulta = RetornaDataConsulta(db, model.ConsultaId); retorno.exAereaOssea = db.ExameViaAereaOssea.Where(c => c.ConsultaId == model.ConsultaId).ToList(); return retorno; } } public DateTime RetornaDataConsulta(winmedEntities db, int consultaId) { var consulta = db.Consulta.Find(consultaId); if (consulta == null) throw new RegraNegocioException("Consulta não encontrada"); return consulta.Data.Value; } } 

The return is used in Web Api Controller and return method used like return Ok(new RegraConsulta().BuscaExamesConsulta(new Consulta { ConsultaId = 1 }));

15
  • @Pac0 so are you saying that to resolve the relation property loading is only set db.Configuration.AutoDetectChangesEnabled = false;? If I used this configuration and I need manipulate in another moment the result of a .ToList()I still able ? Commented Jan 25, 2019 at 13:37
  • That was the belief of the asker of the linked potential duplicate question, but it is not correct. Instead, you'll have to try one of the possible solutions in the various answers. I think in your case you could try the accepted answer (context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; as first line in the using block), and generalize this in some wrapper function. If you find that dirty, my own preference goes for the generic method (most upvoted answer) Commented Jan 25, 2019 at 13:42
  • In the ChangeTracker class doesn't has a property QueryTrackingBehavior and doens't offers a using, only offers void DetectChanges, Enumerable<DbEntitiyEntry> Entries, bool Equals, int GetHashCode, Type GetType, bool HasChanges and ToString methods Commented Jan 25, 2019 at 13:49
  • I tried, and doesn't offers QueryTrackingBehavior Commented Jan 25, 2019 at 13:54
  • 1
    This can only happen if the Consulta is loaded too earlier in the code. Lazy loading absolutely doesn't happen here. Commented Jan 25, 2019 at 19:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.