Getting Started with EF Core on .NET Core Console App with a New database

In this walkthrough, you create a .NET Core console app that performs data access against a SQLite database using Entity Framework Core. You use migrations to create the database from the model. See ASP.NET Core - New database for a Visual Studio version using ASP.NET Core MVC.

Tip

You can view this article's sample on GitHub.

Prerequisites

The .NET Core SDK 2.1

Create a new project

  • Create a new console project:
dotnet new console -o ConsoleApp.SQLite
cd ConsoleApp.SQLite/

Install Entity Framework Core

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

  • Install Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
  • Run dotnet restore to install the new packages.

Create the model

Define a context and entity classes that make up your model.

  • Create a new Model.cs file with the following contents.
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

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

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=blogging.db");
        }
    }

    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 put each class in a separate file and put the connection string in a configuration file. To keep the tutorial simple, everything is contained in one file.

Create the database

Once you have a model, you use migrations to create a database.

  • Run dotnet ef migrations add InitialCreate to scaffold a migration and create the initial set of tables for the model.
  • Run dotnet ef database update to apply the new migration to the database. This command creates the database before applying migrations.

The blogging.db* SQLite DB is in the project directory.

Use your model

  • Open Program.cs and replace the contents with the following code:

    using System;
    
    namespace ConsoleApp.SQLite
    {
        public class Program
        {
            public static void Main()
            {
                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);
                    }
                }
            }
        }
    }
    
  • Test the app:

    dotnet run

    One blog is saved to the database and the details of all blogs are displayed in the console.

    ConsoleApp.SQLite>dotnet run
    1 records saved to database
    
    All blogs in database:
    - http://blogs.msdn.com/adonet
    

Changing the model:

  • If you make changes to your model, you can use the dotnet ef migrations add 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 dotnet ef database update 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.
  • SQLite does not support all migrations (schema changes) due to limitations in SQLite. See SQLite Limitations. For new development, consider dropping the database and creating a new one rather than using migrations when your model changes.

Additional Resources