How to load only the ID attribute in Many-To-One Relationship in EF?

Asked

Viewed 89 times

1

I have two relationships Many-To-One in this class (usuario and projeto):

        public Atividade()
        {
            Projeto = new Projeto();
            StatusAtividade = EStatusAtividade.NaoIniciado;
            TipoAtividade = ETipoAtividade.NovaImplementacao;
            Usuario = new Usuario();
        }

        [JsonConverter(typeof(FormatoDataNullableConverter))]
        public DateTime? DataHoraFim { get; set; }

        [JsonConverter(typeof(FormatoDataNullableConverter))]
        public DateTime? DataHoraInicio { get; set; }

        public string DescricaoAtividade { get; set; }
        public string EstimativaInicialAtividade { get; set; }
        public Projeto Projeto { get; set; }        

        public string NomeProjeto
        {
            get { return Projeto.Nome; }
        }

        public Usuario Usuario { get; set; }

        public string LoginUsuario
        {
            get { return Usuario.Login; }
        }      

        public EStatusAtividade StatusAtividade { get; set; }

        public string DescricaoStatusAtividade
        {
            get { return StatusAtividade.Descricao; }
        }           

        public string DescricaoTipoAtividade
        {
            get { return TipoAtividade.Descricao; }
        }

        public ETipoAtividade TipoAtividade { get; set; }
        public string TituloAtividade { get; set; }


        public override bool Equals(object obj)
        {
            return (obj is Atividade) && (obj as Atividade).Codigo.Equals(Codigo);
        }

        public override int GetHashCode()
        {
            return Codigo.GetHashCode();
        }
    }

In my repository class, set the object load Atividade as follows:

        protected override IQueryable<Atividade> GetEagerLoadConfig()
        {
            return GetModelContext()
                .Include(p => p.Projeto)
                .Include(p => p.Usuario);
        }

        public override Atividade LocalizePorCodigo(long codigo)
        {
            return GetEagerLoadConfig()
                .First(u => u.Codigo.Equals(codigo));
        }

How could you configure for that in the load made in the method GetEagerLoadConfig(), were loaded only the code property of the User and Project objects, rather than fully loading them as is being done? Is it possible?

1 answer

1


No. Not least because there is no need to load only one piece of information, since from a performance standpoint the cost of bringing a column or all is virtually identical.

Moreover, this implementation:

    protected override IQueryable<Atividade> GetEagerLoadConfig()
    {
        return GetModelContext()
            .Include(p => p.Projeto)
            .Include(p => p.Usuario);
    }

    public override Atividade LocalizePorCodigo(long codigo)
    {
        return GetEagerLoadConfig()
            .First(u => u.Codigo.Equals(codigo));
    }

This code has no need. Entity Framework already implements a repository.

  • I understood Gypsy. And in a macro situation (One-To-Many for example) I can use the . Select() method to restrict what I want, right? For in a One-To-Many situation performance would be affected if it carried each object in full.

  • this superscription of public override Atividade LocalizePorCodigo(long codigo) really not necessary, but my 'repository' just encapsulates rules required for my application, and not just implements the standard.

  • @Joaquimmagalhães Restrict within the application, okay. No problems. But no, the performance is very little affected if you carry the object in whole or in parts.

  • @Joaquimmagalhães But you don’t win nothingness with this encapsulation. It is simpler to use the rule directly on DbContext. This is reinvention of the wheel.

  • 1

    Thank you. I will note these points in my studies.

Browser other questions tagged

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