Insert Data By Stored Procedure In MVC 5.0 With Data First Approach

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.

MVC

Step 2

In this step, we will now create Stored Procedure.

MVC

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.

MVC

Step 4

After that, select ADO.NET Entity Data Model and click on Add button.

MVC

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.

MVC

In this way, we have created our model.

MVC

Step 5

Now, we are to select Controller option and create Home Controller. Give the reference of your Model.

MVC

After that, create the object of our connection class.

MVC

Now, we create insertdata method and add new View.

MVC

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.

MVC

Now, your View is ready.

MVC

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).

MVC

Now, change the route from RouteConfig.cs in Solution Explorer.

MVC

And, build the solution of the code. Finally, run it.

MVC

After that, click on "Create".

MVC
Your data is successfully inserted.