Entity Framework
Entity Framework in an ASP.NET is the enhancement of ADO.NET, which provides the strong mapping facilities. It is an ORM (Object Relational Mapping) framework, which gives the ability to the developers to work with various relational databases like SQL Server, Oracle, MYSQL, DB2 etc. and provide the automated mechanism for accessing & storing the data in the database.
There are the 3 approaches given below, which we use during an Application development.
- First, if you want to focus on your domain classes and subsequently create the database from your domain classes; it is called Code First Approach.
- Second, if you already have an existing database and create the domain classes from the database; it is called Database First Approach.
- Third, you want to design your database schema on Visual Designer, followed by creating the database and classes called Model First Approach.
Thus, in this article, we will learn how to develop a simple C# Application, using Entity Framework Code First approach.
Installation
Step 1
After downloading completely, click Install button and it will start the installation process.
Step 2
Once the installation process completes successfully, you will see the dialog given below. Close this dialog and restart your computer, if required.
Step 3
Once everything is done, you will see the main Window of Visual Studio.
Let’s create a new project from File → New → Project.
Go to other Project Types, select Visual Studio Solution and select Blank Soluton. Change solution name CodeFirstApproach, followed by clicking OK button.
Note:- One solution has multiple projects .
Right click on Solution Explorer and New Project and you can see below.
Select class library, followed by clicking OK.
Right click on the project in Solution Explorer and select Manage NuGet Packages.
This will open Manage NuGet packages dialogue box. Now, select Browse on top bar and search for an EntityFramework as shown below.
Select EntityFramework and click Install.
Click I Accept button in License Acceptance dialogue box. This will start the installation.
Now, add a new class file, Employee.cs and declare four properties: Id, LastName, FirstName and BirthDate, as shown below.
- public class Employee
- {
- [DatabaseGenerated(DatabaseGenerationOption.Identity)]
- [Key]
- public int Id {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string FirstName {
- get;
- set;
- }
- public DateTime BirthDate {
- get;
- set;
- }
- }
Add a new class
MyContext.cs and let it inherit from DbContext class (add System.Data.Entity as using).
Now, we will add the connection string in App.config.
- <connectionStrings>
- <add name="Dbconnection" connectionString="Data Source=DHRUV-PC\SQLEXPRESS;Initial Catalog=EmployeeDb;Integrated Security=True" providerName="System.Data.SqlClient" />
- </connectionStrings>
Note
EmployeeDb is the database name, if I use migration and the database will be created automatically i.e. EmployeeDb name.
Now, see Code First Migrations
From the Tools menu, click Library Package Manager and then Package Manager Console then choose the default project EntityDomain in it. That means always choose the project with your MyContext class for migrations.
At PM> enter the command given below.
PM> enable-migrations
When running the preceding command, you will get a console Window, as shown below.
This command adds a new folder, Migrations in the project EntityDomain and this folder contains a configuration file with default settings.
Now, we add to the configuration settings in the Configuration class constructor, i.e. one to allow migration and another for no data loss when migrating. The excerpt of the Configuration class for these properties are given below.
- AutomaticMigrationsEnabled = true;
- AutomaticMigrationDataLossAllowed = false;
We set the AutomaticMigrationEnabled property to true; it means that we are using automatic code first migration and another property AutomaticMigrationDataLossAllowed is set to false. This means that during the migration, no existing data is lost from the migration of the table of the database. The entire Configuration class is shown below.
Thereafter, we will update the database, using Package Manager Console. To update the database at the PM, prompt the command given below.
PM> Update-Database
Now, check in the database. We find that in the database, EmployeeDb with Employees table.
Note
Database name which is provided in app.config and table name, which is the same name as Employee class name.
I hope, this article will provide a clear understanding of how to use Entity Framework in our Application.
I hope, you enjoyed the valuable code. Happy coding.