Entity Framework (EF) is an object-relational mapper (ORM) that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.
Entity Framework allows you to create a model by writing code or using boxes and lines in the EF Designer. Both of these approaches can be used to target an existing database or create a new database.
Entity Framework is the main ORM that Microsoft provides for the .NET Framework and Microsoft’s recommended data access technology.
Version | Release Date |
---|---|
1.0 | 2008-08-11 |
4.0 | 2010-04-12 |
4.1 | 2011-04-12 |
4.1 Update 1 | 2011-07-25 |
4.3.1 | 2012-02-29 |
5.0 | 2012-08-11 |
6.0 | 2013-10-17 |
6.1 | 2014-03-17 |
Core 1.0 | 2016-06-27 |
Release Notes: https://msdn.microsoft.com/en-ca/data/jj574253.aspx
In your Visual Studio open the Solution Explorer window then right click on your project then choose Manage NuGet Packages from the menu:
In the window that opens type EntityFramework
in the search box in the top right.
Or if you are using Visual Studio 2015 you'll see something like this:
Then click Install.
We can also install entity framework using the package manager console. To do you have first to open it using the Tools menu -> NuGet Package Manager -> Package Manager Console then enter this:
Install-Package EntityFramework
This will install Entity Framework and automatically add a reference to the assembly in your project.
Code first allows you to create your entities (classes) without using a GUI designer or a .edmx file. It is named Code first, because you can create your models first and Entity framework will create database according to mappings for you automatically. Or you can also use this approach with existing database, which is called code first with existing database For example, if you want a table to hold a list of planets:
public class Planet
{
public string Name { get; set; }
public decimal AverageDistanceFromSun { get; set; }
}
Now create your context which is the bridge between your entity classes and the database. Give it one or more DbSet<>
properties:
using System.Data.Entity;
public class PlanetContext : DbContext
{
public DbSet<Planet> Planets { get; set; }
}
We can use this by doing the following:
using(var context = new PlanetContext())
{
var jupiter = new Planet
{
Name = "Jupiter",
AverageDistanceFromSun = 778.5
};
context.Planets.Add(jupiter);
context.SaveChanges();
}
In this example we create a new Planet
with the Name
property with the value of "Jupiter"
and the AverageDistanceFromSun
property with the value of 778.5
We can then add this Planet
to the context by using the DbSet
's Add()
method and commit our changes to the database by using the SaveChanges()
method.
Or we can retrieve rows from the database:
using(var context = new PlanetContext())
{
var jupiter = context.Planets.Single(p => p.Name == "Jupiter");
Console.WriteLine($"Jupiter is {jupiter.AverageDistanceFromSun} million km from the sun.");
}
Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called "Entity Framework" to automate database related activities for your application.
Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.
What is O/RM?
ORM is a tool for storing data from domain objects to the relational database like MS SQL Server, in an automated way, without much programming. O/RM includes three main parts:
ORM allows us to keep our database design separate from our domain class design. This makes the application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so that the developer doesn't need to write it manually.