How to map the results of a store procedure with an entity using Entity Framework?

2

Speak up. I'm doing a project with MVC 4 and EF6.

I would call a proc and its results populate an entity. But my entity is not a database table, so I did not use Dbset on it. What happens is that this proc joins some tables. I am doing everything via code, because the bank already existed and there is a lot that I will not use.

This is my entity

public class Dados
{
    public string dtAnoMes { get; set; }
    public string valor{ get; set; }
    public string valorTotal { get; set; }
    public string dif { get; set; }

    public Dados()
    { }

    public IList<Dados> RecuperaValores(string data)
    {
        using (var context = new ContextoBanco())
        {
            var dtInicio = new SqlParameter("@Value1", data);
            var dtFim = new SqlParameter("@Value2", data);

            var result = context.Database.SqlQuery<Dados>("SP_RECUPERA_VALORES @Value1, @Value2", dtInicio , dtFim ).ToList();


            return result.ToList();
        }
    }

}

My proc returns four columns: YEAR / MONTH, VALUE, TOTAL_VALUE, DIFFERENCE.

I wanted to somehow say that the YEAR / MONTH column equals the entity's dTANoMes property and so on. The proc code is working fine. only this map is missing. Does anyone know how I can do it?

Thank you!

UPDATED

I was able to resolve upgrading my Entity Framework to version 6, and using MapToStoredProcedures. But I'm still looking for an annotation that maps my property to the proc's back column, just by perfumery myself.

    
asked by anonymous 21.06.2017 / 22:35

1 answer

1

You can map a DbSet to a storedprocedure in context

public DbSet<Dados> Dados { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Dados>().MapToStoredProcedures();
}

I'll leave this Renato Haddad file on MSDN as a reference by talking about Stored Procedures in EF: link

a>

    
21.06.2017 / 22:49