Welcome To Learn Entity Framework Core

This site provides documentation and tutorials for people looking for help with using Entity Framework Core, Microsoft's recommended data access technology for applications based on the .NET Core framework.

What is Entity Framework Core?

Entity Framework Core (EF Core) is the latest version of the Entity Framework from Microsoft. 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))

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