.NET Core Entity Framework

11/16/2019 Jeff
As of .NET Core 2.2 & Visual Studio 2017, Entity Framework is not initially configured. Fortunately it's not difficult to setup. Here are the steps I used.

NuGet Installations

Install the following via NuGet:
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

Create the Model classes

You can create the models manually, or via an automated process. For a good example of the Database-First generation steps, see here.

Here is an example class which maps to my BankAccount table:

    public partial class BankAccount
    {
        public BankAccount()
        {
            this.BankAccountTransactions = new HashSet<BankAccountTransaction>();
        }
        
        public int BankAccountID { get; set; }
        public string Name { get; set; }
        public string AccountType { get; set; }
    
        public virtual ICollection<BankAccountTransaction> BankAccountTransactions { get; set; }
    }

Create the DbContext

Unless you auto-generated your classes in the step above, manually create a class called AppDbContext (or similarly named):

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    public virtual DbSet<BankAccount> BankAccounts { get; set; }
    public virtual DbSet<BankAccountTransaction> BankAccountTransactions { get; set; }
}

Configure Sql Server

Listed here are two way to configure SQL Server. The first is the quick-and-dirty, and worth implemented as a quick test. The second is the preferred implementation.

The quick-and-dirty

The quickest way to configure SqlServer is to add the following function to your derived DbContext class (in my case to my AppDbContext class):

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(    
            @"Server=.\;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");
    }

Configuring SqlServer: The preferred way

Add a connection string

In the appsettings.json file add a connection strings section as such:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Quid_Dev;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  ...
}
Configure the Startup.cs

In ConfigureServices call, add a call to AddDbContextPool. This is preferred over AddDbContext because most likely you will be creating multiple instances of AddDbContext, and you’ll want these pooled. Notice we’re also telling it to use SqlServer, also with the connection string from our appsettings.json.

    services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));

Get Some Data

In one of your Controller functions, add the following code and run it in the debugger:

    using (AppDbContext context = new AppDbContext(new DbContextOptions<AppDbContext>()))
    {
        var bankAccounts = context.BankAccounts.ToList();

        foreach (var ba in bankAccounts)
        {
            Debug.WriteLine($"Account: {ba.Name}");
        }
    }

That should be all that’s needed. Happy coding…


Please register or login to add a comment.

Comments (displaying 1 - 1):
No comments yet! Be the first...


  • C#/.NET
  • T-SQL
  • HTML/CSS
  • JavaScript/jQuery
  • .NET 8
  • ASP.NET/MVC
  • Xamarin/MAUI
  • WPF
  • Windows 11
  • SQL Server 20xx
  • Android
  • XBox
  • Arduino
  • Skiing
  • Rock Climbing
  • White water kayaking
  • Road Biking