Introduction
In this article, we will learn how to use stored procedure in MVC 5.0 with Data First Approach.
Step 1
Create a database and create a table.
Step 2
In this step, we will now create Stored Procedure.
Step 3
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.
Step 4
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.
Step 5
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.