Execute procedure with CodeFirst

1

I'm trying to run a procedure in ASP.NET MVC with CodeFirst. I saw some examples on the internet but they are not working, can anyone help me?

    public void Copiadados(int Cod_Cli, int Cod_Obra)
    {
        SqlParameter p1 = new SqlParameter("@Cod_Cli", SqlDbType.Int);
        SqlParameter p2 = new SqlParameter("@Cod_Obra", SqlDbType.Int);

        p1.Value = Cod_Cli;
        p2.Value = Cod_Obra;

        Database.ExecuteSqlCommand("ProcName @Cod_Cli, @Cod_Obra", p1, p2);
    }
    
asked by anonymous 13.08.2014 / 19:55

1 answer

2
  

Warning : Running Stored Procedures on projects that use Entity Framework Code First can be considered a bad practice, since the logic that should be in the application will be in the database, causing inconsistencies and bad system.

Your code is almost right. The word 'context' was missing before Database .

public void Copiadados(int Cod_Cli, int Cod_Obra)
{
    SqlParameter p1 = new SqlParameter("@Cod_Cli", SqlDbType.Int);
    SqlParameter p2 = new SqlParameter("@Cod_Obra", SqlDbType.Int);

    p1.Value = Cod_Cli;
    p2.Value = Cod_Obra;

    context.Database.ExecuteSqlCommand("ProcName @Cod_Cli, @Cod_Obra", p1, p2);
}
    
13.08.2014 / 22:00