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

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

View this article's sample on GitHub.

Prerequisites

Create a new project

  • Open Visual Studio 2017

  • File > New > Project...

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

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

  • Make sure that the project targets .NET Framework 4.6.1 or later

  • Name the project ConsoleApp.NewDb and click OK

Install Entity Framework

To use EF Core, install the package for the database provider(s) you want to target. This tutorial 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 tutorial you use some Entity Framework Tools to maintain the database. So install the tools package as well.

  • Run Install-Package Microsoft.EntityFrameworkCore.Tools

Create the model

Now it's time to define a context and entity classes that make up the 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 ConsoleApp.NewDb
    {
        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 a configuration file or environment variable. For the sake of simplicity, everything is in a single code file for this tutorial.

Create the database

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

  • Tools > NuGet Package Manager > Package Manager Console

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

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

Tip

If you make changes to the 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 the model

You can now use the model to perform data access.

  • Open Program.cs

  • Replace the contents of the file with the following code

    using System;
    
    namespace ConsoleApp.NewDb
    {
        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 see that one blog is saved to the database and then the details of all blogs are printed to the console.

    image

Additional Resources