In this article, we will learn how to use stored procedure in MVC 5.0 with Data First Approach.
Create a database and create a table.
In this step, we will now create Stored Procedure.
In the next step, we connect database to our application through Data First Approach.
Open Solution Explore and select-> Model. Right click and select "Add" and after that, select New item.
After that, select ADO.NET Entity Data Model and click on Add button.
After that, Entity Data Model Wizard opens. Select "Generate from database" option and click "Next". And here, will be created a new connection. Click on "Next" and choose Entity Framework 5.0 >> Next. Here, select your ->Table and ->Stored Procedure and click on Finish button.
In this way, we have created our model.
Now, we are to select Controller option and create Home Controller. Give the reference of your Model.
After that, create the object of our connection class.
Now, we create insertdata method and add new View.
Now, open Add View, give the View name, and choose "Create Template". Select the model name and select the Data Context Class and followed by a click on Add button.
Now, your View is ready.
Now, come back on your HomeController and here, create another Insertdata method which is decorated by HttpPost . After that, we need to call our studins method from Data Context (demo2Entities).
Now, change the route from RouteConfig.cs in Solution Explorer.
And, build the solution of the code. Finally, run it.
After that, click on "Create".
Your data is successfully inserted.