Run Trial with Codefirst

Asked

Viewed 120 times

1

I am trying to run a trial on ASP.NET MVC with Codefirst. I saw some examples on the internet but they are not working, someone can help me?

Follow the code I’m using, the Procedure just copies some data between tables and returns nothing.

    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);
    }
  • Why you have to run a Stored Procedure on an Entity Framework project?

  • When a work is registered, the billing of the work can be different from the customer’s data or not, when it is different the user marks a checkbox and the fields to fill in the data are opened on the screen, when the data is the same as the previous client will copy the data to the billing table.

  • And this business rule is impossible to implement in a Controller? Some information that will be required for this copy is not recoverable by context?

  • See, I can put the answer, but I want to be able to disagree a little bit more about how it’s being done. I believe that is not the correct approach to solve your problem.

  • I understand, is that I thought the best way was this, I could in the controller do the logic to search the data in the database and create an object and register this object, I did not expect to be too complex to run this process.

  • I did as I said in the above comment, in the controller I search the customer data I need and register in the billing table.

  • So. It’s best to put logic in Controller. Still, you want to keep this execution of Stored Procedure?

  • No, I’ve already done it for the controller, but if you have the code for how to run the process and can put it here would be good, it may be necessary some day.

  • Business logic in the controller??? Sorry, but are implementing wrong.

Show 4 more comments

1 answer

2


Warning: Running Stored Procedures on projects using Entity Framework Code First can be considered a bad practice, since the logic that should be in the application will be in the database, which can cause inconsistencies and malfunctioning of your system.

Your code is almost right. Missed the word 'context' 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);
}

Browser other questions tagged

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