Entity Framework Core [.net core] Tutorial

In this tutorial you will learn how to work with new Entity Framework in .net core framework, Though most of the functionalities will remain same as earlier framework, but there are some interesting new features in EF .net Core.

Must read about Entity Framework .Net Core

To start working with Entity framework Core with any type of .Net Core application, first you need to install Microsoft.EntityFrameworkCore library from Nuget package manager, so right click on your project solution and then open nuget manager, search for any library you want to install, then from search result select the library and install it.

Now create a DbContext file inherited from Microsoft.EntityFrameworkCore.DbContext class.

public class EFContext : Microsoft.EntityFrameworkCore.DbContext
{

}

To work with SQL server database we need to use UseSqlServer(conString) method in configuration, so simply override the OnConfiguring method as example below.

Now you may not find usesqlserver method in .net core 3.1 version, so we need to install an additional package Microsoft.EntityFrameworkCore.SqlServer using nuget package manager, after installing the package you will be able to use the extention method UseSqlServer("connection-string") as shown in code below.

Make sure you have added namespace using Microsoft.EntityFrameworkCore; in you class file.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)            
    {                
        optionsBuilder.UseSqlServer(conString);
    }
    base.OnConfiguring(optionsBuilder);
}

You can build an ASP.NET Core MVC application that performs basic data access using Entity Framework Core.

Prerequisites
  • Visual Studio 2017 or later with these workloads
    • .NET Core cross-platform development
    • ASP.NET and web development
  • .NET Core 2.1 SDK.
Things to learn in Entity Framework Core
EF Core vs EF 6

Entity Framework Core is the new form of ORM and improved version of Entity Framework 6 for .NET Core Framework. EF Core supports two database approaches

  1. Code-First
  2. Database-First
EF Core mainly designed for code-first approach and provides little support for the database-first approach because, EF Core does not provide support for visual designer or wizard for DB model, like we used to have EF 6

EF Core Versions
  • EF Core 2.0
    2017-August
  • EF Core 1.1
    2016-November
  • EF Core 1.0
    2016

Here are few important links for Entity Framework Core

New features in EF Core

Here are some new features of EF Core which were not supported in earlier EF versions

  • Batch INSERT, UPDATE, DELETE operations
  • Support for IoC (Inversion of Control)
  • Alternate keys
  • In-memory provider for testing
  • Easy relationship configuration
Here are the differences and complete Comparison Entity Framework Core with EF 6

EF Core Database Providers

Entity Framework Core uses different provider models for accessing different databases. You can install EF Core providers using NuGet packages

  • SQL Server, Package: Microsoft.EntityFrameworkCore.SqlServer
  • MySQL, Package: MySql.Data.EntityFrameworkCore
  • SQLite, Package: Microsoft.EntityFrameworkCore.SQLite
  • PostgreSQL, Package: Npgsql.EntityFrameworkCore.PostgreSQL

 
Entity Framework Core
Learn entity framework orm using c#, entity framework core and earlier version of entity framework, all tutorials are written in c#.
Entity Framework Interview Questions

SQL interview questions
Entity Framework C# Examples | Join .Net C# Course