In this article, we’ll learn how to perform CRUD operations with a code first approach in an MVC application. Here, we will create an MVC structure that will help to change in the Model Class and that change will update it in the database. Microsoft’s Entity Framework (EF) avoids working directly with the database and creates the database per the model classes’ requirements. We will not use visual model designer (EDMX) completely but will create POCO classes first and then create the database from these POCO classes.
Open Visual Studio and select “File” >> “New”. Then click on Project (remember, don’t go with the option ‘File->New->Website’).
Select “Templates” >> Visual C# >> Web, then ASP.NET Web Application (.NET Framework), and give it an appropriate project name.
Step 3
And here is your default screen.
Here, we will create the basic structure which looks professional and easy to use.
Right click on Solution >> Add >> New Project.
Now, we will add our Code-First-Demo.Repository class library, which contains our migration files.
After adding the project, delete the default class named Class1.cs.
Right-click on Code-First-Demo.Repository >> Add and add a new class.
Click on the class and give it an appropriate name. Here, I’ll set the name to ‘CDBContext.’
Now, add a reference to EntityFramework, add the namespace “using System.Data.Entity” set the access specifier to the public, and extend from DbContext
.
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Code_First_Demo.Repository { public class CDBContext : DbContext { } }
Now, add your connection string in the Web.config file of the main (Code-First-Demo) project.
<connectionStrings> <add name="StringDBContext" connectionString="Server=FAISAL-PATHAN\SQLEXPRESS;Initial Catalog=DemoDB;Persist Security Info=False;User ID=sa;Password=***;MultipleActiveResultSets=True;Encrypt=False;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" /> </connectionStrings>
Create a constructor, extend it from the base class, and pass it in.
Right click on Solution, and add a new project.
Now, we will add our Code-First-Demo.Model class library, which contains all our classes.
After this, delete the default class named Class1.cs.
Now, we must add a Code-First-Demo.Model class library reference to Code-First-Demo.Repository class library in order to access and generate a table in our SQL Server.
Add a Code-First-Demo.Model class library reference to Code-First-Demo.Repository class library. Right-click on Code-First-Demo.Repository, Select Add > Reference.
Now, select Code-First-Demo.Model and click OK.
In Code-First-Demo.Repository class library, you can see a Code-First-Demo.Model reference.
Now, we are adding our class to Code-First-Demo.Model to create our SQL table.
Add properties to the class as per the columns you want in the table.
MyFirstTable.cs class
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Code_First_Demo.Models { public class MyFirstTable { [Key] public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } }
Add a namespace using Code_First_Demo.Models.
CDBContext.cs
using Code_First_Demo.Models; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Code_First_Demo.Repository { public class CDBContext : DbContext { public CDBContext() : base("StringDBContext") { } public DbSet<MyFirstTable> MyFirstTable { get; set; } } }
Now, open the package manager console to fire the commands.
Enables Code First Migrations in the project.
Syntax
Scaffold a migration script for any pending model changes.
Apply any pending migrations to the database.
Type/select the below commands and press enter:
Install-Package EntityFramework
This will add EntiryFramwork related packages.
Enable-Migrations
Note: You can put any migration name you choose.
This command will create your migration file based on the model’s properties.
Note: This command will Create/modify your database when a connection string is mentioned in the web.config file.
Now rebuild the solution and perform the below action to perform CRUD operations.
Add a reference to both the class library projects to the main web project.
Now add a new controller for CRUD operations.
Right click on controllers, Add and Controller…
Select “MVC 5 controller with views, using entity framework,” and click on the Add button.
Here we did the Code-First approach in MVC application in an easy way.
Display Page (List of Users)
Edit Page
Delete Page
Here, we created a simple application that uses the Entity Framework and a SQL Server to perform CRUD operations with a Code First approach. In the following article, we will see how to do basic CRUD operations. You can leave feedback/comments/questions to this article. Please let me know how you like and understand this article and how I could improve it.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
View Comments
Thanks for the easy-understandable Explanation.
very nice explanation