Getting started with EF Core on .NET Framework with a New Database

In this walkthrough, you will build a console application that performs basic data access against a Microsoft SQL Server database using Entity Framework. You will use migrations to create the database from your model.

Tip

You can view this article's sample on GitHub.

Prerequisites

The following prerequisites are needed to complete this walkthrough:

Create a new project

  • Open Visual Studio

  • File > New > Project...

  • From the left menu select Templates > Visual C# > Windows Classic Desktop

  • Select the Console App (.NET Framework) project template

  • Ensure you are targeting .NET Framework 4.5.1 or later

  • Give the project a name and click OK

Install Entity Framework

To use EF Core, install the package for the database provider(s) you want to target. This walkthrough uses SQL Server. For a list of available providers see Database Providers.

  • Tools > NuGet Package Manager > Package Manager Console

  • Run Install-Package Microsoft.EntityFrameworkCore.SqlServer

Later in this walkthrough we will also be using some Entity Framework Tools to maintain the database. So we will install the tools package as well.

  • Run Install-Package Microsoft.EntityFrameworkCore.Tools

Create your model

Now it's time to define a context and entity classes that make up your model.

  • Project > Add Class...

  • Enter Model.cs as the name and click OK

  • Replace the contents of the file with the following code

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFGetStarted.ConsoleApp
{
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

Tip

In a real application you would put each class in a separate file and put the connection string in the App.Config file and read it out using ConfigurationManager. For the sake of simplicity, we are putting everything in a single code file for this tutorial.

Create your database

Now that you have a model, you can use migrations to create a database for you.

  • Tools –> NuGet Package Manager –> Package Manager Console

  • Run Add-Migration MyFirstMigration to scaffold a migration to create the initial set of tables for your model.

  • Run Update-Database to apply the new migration to the database. Because your database doesn't exist yet, it will be created for you before the migration is applied.

Tip

If you make future changes to your model, you can use the Add-Migration command to scaffold a new migration to make the corresponding schema changes to the database. Once you have checked the scaffolded code (and made any required changes), you can use the Update-Database command to apply the changes to the database.

EF uses a __EFMigrationsHistory table in the database to keep track of which migrations have already been applied to the database.

Use your model

You can now use your model to perform data access.

  • Open Program.cs

  • Replace the contents of the file with the following code

using System;

namespace EFGetStarted.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
                var count = db.SaveChanges();
                Console.WriteLine("{0} records saved to database", count);

                Console.WriteLine();
                Console.WriteLine("All blogs in database:");
                foreach (var blog in db.Blogs)
                {
                    Console.WriteLine(" - {0}", blog.Url);
                }
            }
        }
    }
}
  • Debug > Start Without Debugging

You will see that one blog is saved to the database and then the details of all blogs are printed to the console.

image