Entity Framwork

What is Entity Framework? 

ADO.NET EF is an ORM (object-relational mapping) which creates a higher abstract object model over ADO.NET components. So rather than getting into dataset, datatables, command, and connection objects as shown in the below code, you work on higher level domain objects like customers, suppliers, etc.

What is an ADO.Net?

Pros and Cons of Entity Framework

List of Pros of Entity Framework

1. Reduces Development Time.
Since Entity Framework enable developers to work with the data in the form of domain-specific objects and properties without dealing with the data engines used to store the data, software applications can be created in less time.

2. Reduces Development Cost.
Closely related to reducing development time (above), applications are less expensive to develop because the architects of data-oriented applications spend less manhours building the applications.

3. Provides a Common Syntax.
It provides unique syntax (LINQ/Yoda) for all object queries, both inside and outside of the database.

List of Cons of Entity Framework

1. Lack of Certain Functionality.
Users report that it does not perform well when working with large domain models.  Another issue is poorly written SQL queries that are slow running and need reworking as a SP or View. Also, some say that there are issues with computed values or values that are added in an Insert Trigger being left out of the model after a new record is added.

2. Includes Extra Data.
When using class models, some people say that Entity Framework includes additional unnecessary data which makes the class sizes bigger.

3. Data Migrations Break Easily.
The data migration functionality is weak, and so, in practice, it just doesn’t work well enough.

What is Entity Framework Core ?

 It has been designed to be lightweight, extensible and to support cross platform development as part of Microsoft’s .NET Core framework. It has also been designed to be simpler to use, and to offer performance improvements over previous versions of Entity Framework.

EF Core is an object-relational mapper (ORM). Object-relational mapping is a technique that enables developers to work with data in object-oriented way by performing the work required to map between objects defined in an application’s programming language and data stored in relational datasources.

Why use an ORM?

Most development frameworks include libraries that enable access to data from relational databases via recordset-like data structures. The following code sample illustrates a typical scenario where data is retrieved from a database and stored in an ADO.NET DataTable so that it is accessible to the program’s code:

using(var conn = new SqlConnection(connectionString))using(var cmd = new SqlCommand("select * from Products", conn)){    var dt = new DataTable();    using(var da = new SqlDataAdapter(cmd))    {        da.Fill(dt);    }}

The data within the DataTable is accessible via numeric or string indexers and needs to be converted from object to the correct type:

foreach(DataRow row in dt.Rows)
    int productId = Convert.ToInt32(row[0]);  
  string productName = row["ProductName"].ToString();

This late-bound or “weakly-typed” approach to data access is prone to error. Problems commonly arise from mistyping the name of a column, or finding that the name of the column has been changed in the database, or from a change to the order in which fields are specified in the SQL statement without a corresponding change being made to the application code. Equally, data type conversions might fail. The code will still compile, but will error at runtime. Consequently, professional developers prefer to work with data in a strongly-typed manner.

Strong Typing

When you take a strongly-typed approach to data, you work with properties of predefined classes that form a domain model in an object-oriented way:

public class Product{ 
   int ProductId { get; set; }  
  string ProductName { get; set; }
int productId = myProduct.ProductId;
string productName = myProduct.ProductName;

Work still needs to be done to retrieve and map the data from the database to an instance of the domain object. One option is to write your own code to manage this. However, as the domain model grows, the amount of code required can grow, and will need more and more development time to maintain. This will increase the overall amount of time required to complete an application.

ORMs are pre-written libraries of code that do this work for you. Full-featured ORMs do a lot more too. They can

  • map a domain model to database objects
  • create databases and maintain the schema in line with changes to the model
  • generate SQL and execute it against the database
  • manage transactions
  • keep track of objects that have already been retrieved


  • > dotnet new mvc
  • > dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  • > dotnet add package --version 1.1.0-msbuild3-final Microsoft.EntityFrameworkCore.Tools
  • > dotnet restore
  • > dotnet run
  • dotnet ef -h
  • dotnet ef migrations add CreateDatabase
  • dotnet ef database update
  • dotnet ef migrations add LimitStrings
  • dotnet ef database update