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

1

I have two Many-To-One relationships 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, I set the load of the object 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 set up so that in the load made in the GetEagerLoadConfig() method, the property of the User and Project objects would only be loaded just , instead of loading them completely as it is done? Is it possible?

    
asked by anonymous 17.07.2015 / 16:40

1 answer

1

No. Even because there is no need to only load information, since from the point of view of performance the cost of bringing a column or all is virtually identical.

Incidentally, 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 does not have the slightest need. The Entity Framework already implements a repository .

    
17.07.2015 / 17:20