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

Felippe Tadeu
  • 237
  • 2
  • 11
  • @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 ? – Felippe Tadeu Jan 25 '19 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) – Pac0 Jan 25 '19 at 13:42
  • In the ChangeTracker class doesn't has a property QueryTrackingBehavior and doens't offers a using, only offers void DetectChanges, Enumerable Entries, bool Equals, int GetHashCode, Type GetType, bool HasChanges and ToString methods – Felippe Tadeu Jan 25 '19 at 13:49
  • I tried, and doesn't offers `QueryTrackingBehavior` – Felippe Tadeu Jan 25 '19 at 13:54
  • yes, my bad, this feature is available for for EF Core but not EF 6 apparently. – Pac0 Jan 25 '19 at 13:55
  • Can you exclude the possible duplicated ? – Felippe Tadeu Jan 25 '19 at 13:57
  • Yep, your question is version specific. Done. Good luck for your searches – Pac0 Jan 25 '19 at 13:59
  • I added the more general Entity-Framework tag, as it doesn't really is about asp.net and could get you more visibility. – Pac0 Jan 25 '19 at 14:00
  • What about `db.Configuration.LazyLoadingEnabled = true;` just after the declaration of db ? – Antoine Pelletier Jan 25 '19 at 16:29
  • But the question is not use LazyLoading, even so i set to false still load the Consulta relation property – Felippe Tadeu Jan 25 '19 at 18:13
  • 1
    This can only happen if the `Consulta` is loaded too earlier in the code. Lazy loading absolutely doesn't happen here. – Gert Arnold Jan 25 '19 at 19:33
  • You have to make this property VIRTUAL https://stackoverflow.com/questions/41881169/navigation-property-should-be-virtual-not-required-in-ef-core/41881389#41881389 Lots of info there i suggest you to read carefully – Antoine Pelletier Jan 25 '19 at 19:39
  • @GertArnold please, write your comment as a answer, please. If possible what can I do to avoid this behavior, I thinked in two ways, using a new connection or write a raw query – Felippe Tadeu Jan 25 '19 at 22:04
  • But your controller code doesn't show where the described behavior occurs, or it should be in `consulta.Data.Value`, but it's not clear what `consulta.Data` is. – Gert Arnold Jan 26 '19 at 18:32
  • @GertArnold `consulta.Data` is a DateTime property that I need to show in client and I retrieved using Find method with the same opened connection on method `BuscaExamesConsulta` – Felippe Tadeu Jan 27 '19 at 21:30

0 Answers0