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.
Thanks for the answer. But strange, I do not have this maptostoredprocedures rs.
– Guilherme Camarotto
What version of Entity are you using? Because these calls exist in EF 6 forward
– Grupo CDS Informática
I think that was it, was in the version 5 rsrs. thank you very much.
– Guilherme Camarotto
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.
– Guilherme Camarotto
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.
– Grupo CDS Informática
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.
– Guilherme Camarotto
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.
– Guilherme Camarotto