In this tutorial, we will learn about ASP.NET MVC Entity Framework Code First approach. We will understand how to use a database engine in order to save, Edit, Update, and delete the data needed for your application using ASP.NET MVC Entity Framework. The ASP.NET MVC Entity Framework(EF) supports code first approach, which allows you to create model objects by writing simple classes. And the database will be created on the basis of your classes. This is very fast and clean development workflow.
How to work with ASP.NET MVC Entity Framework?
Let’s take a look at a simple example by creating a new project.
ð Create a new ASP.NET MVC empty project with the name of “DatabaseDemo”.
Note: – If you don’t know how to create a new ASP.NET MVC empty project, then see the previous tutorial.
ð Install Entity Framework
To install the Entity Framework, just right-click on your project name in the right pane and choose “Manage NuGet Packages…”. (Right click on project name => Manage NuGet Packages).
Then it will open the NuGet Package Manger. Search Entity Framework in the search box. Then select the Entity Framework and then click ‘Install’ button. It will show a ‘License Acceptance’ dialog.
Just click the ‘I Accept’ button. It will start the installation.
Once the Entity Framework installed you will see the message in the output window.
ð Add DbContext Class
Right-click on the model folder in the right pane, choose add option and then choose a class (Right-Click model folder => Add => Class).
Enter the name “Studetn.cs” in the name field and then click ok. It will show a new class student.cs under the Model folder.
Now write the following code in Student.cs class, which will communicate with the entity framework to save, update, edit, and delete the data.
In this code StudentDBContext class is derived from DbContext class. If you want to know what actually DbContext is, then right-click on it and then choose “Go to Definition”.
Note: – if you see an error or a red line just under the “DbContext” and “DbSet<Student>”, then just make sure you have added the “using System.Data.Entity” library just above the namespace.
ð Connection String
Now open web.config file. If you want to add connection string, then add the following Connection String under the configuration tag.
Note:- You don’t actually need to add the StudentDBContext connection string. If you don’t specify a connection string, Entity Framework will create a localDB database in the user’s directory. So we will not add connection string in this project.
ð Rebuild your solution
Press Ctrl + Shift + B to rebuild your project.
ð Add Controller
Right click on the controller folder in the solution explorer, select Add, and the choose Controller (Right click controller folder => Add => Controller). It will show an “Add Controller” dialog.
Enter the name of the controller (E.g. StudentsController.cs) in the Controller name field and click Add button. It will show a new dialog.
Then Choose “MVC 5 Controller with Views, using Entity Framework” option, which will create Save, Delete, Update, and Edit code for you, and then click Add button. It will show an ‘Add Controller’ dialog.
Then choose the “Student(DataBaseDemo.Models)” class from the ‘Model Class’ drop-down list. And then choose “StudentDBContext(DataBaseDemo.Models)” class from the ‘Data Context Class’ drop-down list. And finally, click ‘Add’ button.
Note: – If you get an error, then simply rebuild your project and add a controller again through above process.
Now you will see a StudentsController.cs class with some code. This code is created by Entity Framework automatically for saving, Editing, Updating and deleting the record from the database.
Now run your application with this URL http://localhost:54081/Students/index. You will see the following output.
As you can see there is no record on the view, this is because we have not added any record in our database which is actually created by Visual Studio. Let’s go to “Server Explorer”, there you will see a database is created with the same name as we have in DbContext class.
To see the data in the table, right-click on the Students table and select “Show Table Data”.
Now you will see we have no record in our database table.
Let’s add some record in the database directly.
Then refresh the browser, and then you will see that the data is now updated to the view from the database.
Now let’s add some record from the browser by clicking the “Create New” link. It will show the create view page. So, now enter the record and then click on Create button.
Now it will update the index view as well add this new record to the database.
Note:- you can also update, delete and edit the record in this ASP.NET MVC Entity Framework project.