Getting started with Entity Framework
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.
|4.1 Update 1||2011-07-25|
Release Notes: https://msdn.microsoft.com/en-ca/data/jj574253.aspx
Installing The Entity Framework NuGet Package
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:
This will install Entity Framework and automatically add a reference to the assembly in your project.
Using Entity Framework from C# (Code First)
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:
Now create your context which is the bridge between your entity classes and the database. Give it one or more
We can use this by doing the following:
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
We can then add this
Planet to the context by using the
Add() method and commit our changes to the database by using the
Or we can retrieve rows from the database:
What is Entity Framework ?
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:
- Domain class objects
- Relational database objects
- Mapping information on how domain objects map to relational database objects(e.x tables, views & stored procedures)
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.