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

Asked

Viewed 293 times

2

Personal talk. I’m doing a project with MVC 4 and EF6.

I’d like to call in a proc and her results populate an entity. But my entity is not a bank table, so I did not use Dbset on it. What happens is that this proc makes Join of some tables. I’m doing everything by code, because the bank already existed and there’s a lot I won’t 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, VALOR_TOTAL, DIFFERENCE.

wanted to somehow say that the column YEAR/MES equals the entity’s dtANoMes property and so by diants. The code to execute proc is working fine. only this map is missing. Does anyone know how to do it?

Thank you very much!

UPDATED

I was able to solve it by updating my Entity Framework to version 6, and using Maptostoredprocedures. But I’m still looking for an Annotation that envelops my property with the return column of proc, just by perfumery same.

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 will leave as reference this file of Renato Haddad in MSDN talking about Stored Procedures in EF: https://msdn.microsoft.com/pt-br/library/dn720414.aspx

  • Thanks for the answer. But strange, I do not have this maptostoredprocedures rs.

  • What version of Entity are you using? Because these calls exist in EF 6 forward

  • I think that was it, was in the version 5 rsrs. thank you very much.

  • vixi, now you are saying that my Data entity does not have a defined key. and really I don’t have a key, because this entity is not a table, I created it just to receive the data from proc.

  • If we look at the Ef convention, putting a column with Id in the Precedent will solve it. If YEAR/MES does not repeat, put the Attribute [Key] above the property that the EF will understand that it is the key.

  • got it. The anome column repeats a lot. None of the columns q to proc turn is single. I’ll create a property in the entity to be the key and see if I can change the proc to return a single column.

  • Thank you very much. it worked out here. I created a prop id and put a row_number as id in my proc, then he mapped. I just need now to map the other props with the other columns, I didn’t want to put the same name in the props, but if there’s no way I leave the same names.

Show 2 more comments

Browser other questions tagged

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