Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
164 lines (101 sloc) 5.39 KB
title author ms.date ms.assetid uid
Getting Started - EF Core
rick-anderson
09/17/2019
3c88427c-20c6-42ec-a736-22d3eccd5071
core/get-started/index

Getting Started with EF Core

In this tutorial, you create a .NET Core console app that performs data access against a SQLite database using Entity Framework Core.

You can follow the tutorial by using Visual Studio on Windows, or by using the .NET Core CLI on Windows, macOS, or Linux.

View this article's sample on GitHub.

Prerequisites

Install the following software:

.NET Core CLI

Visual Studio


Create a new project

.NET Core CLI

dotnet new console -o EFGetStarted
cd EFGetStarted

Visual Studio

  • Open Visual Studio
  • Click Create a new project
  • Select Console App (.NET Core) with the C# tag and click Next
  • Enter EFGetStarted for the name and click Create

Install Entity Framework Core

To install EF Core, you install the package for the EF Core database provider(s) you want to target. This tutorial uses SQLite because it runs on all platforms that .NET Core supports. For a list of available providers, see Database Providers.

.NET Core CLI

dotnet add package Microsoft.EntityFrameworkCore.Sqlite

Visual Studio

  • Tools > NuGet Package Manager > Package Manager Console

  • Run the following commands:

    Install-Package Microsoft.EntityFrameworkCore.Sqlite

Tip: You can also install packages by right-clicking on the project and selecting Manage NuGet Packages


Create the model

Define a context class and entity classes that make up the model.

.NET Core CLI

  • In the project directory, create Model.cs with the following code

Visual Studio

  • Right-click on the project and select Add > Class
  • Enter Model.cs as the name and click Add
  • Replace the contents of the file with the following code

[!code-csharpMain]

EF Core can also reverse engineer a model from an existing database.

Tip: In a real app, you put each class in a separate file and put the connection string in a configuration file or environment variable. To keep the tutorial simple, everything is contained in one file.

Create the database

The following steps use migrations to create a database.

.NET Core CLI

  • Run the following commands:

    dotnet tool install --global dotnet-ef
    dotnet add package Microsoft.EntityFrameworkCore.Design
    dotnet ef migrations add InitialCreate
    dotnet ef database update
    

    This installs dotnet ef and the design package which is required to run the command on a project. The migrations command scaffolds a migration to create the initial set of tables for the model. The database update command creates the database and applies the new migration to it.

Visual Studio

  • Run the following commands in Package Manager Console

    Install-Package Microsoft.EntityFrameworkCore.Tools
    Add-Migration InitialCreate
    Update-Database

    This installs the PMC tools for EF Core. The Add-Migration command scaffolds a migration to create the initial set of tables for the model. The Update-Database command creates the database and applies the new migration to it.


Create, read, update & delete

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

    [!code-csharpMain]

Run the app

.NET Core CLI

dotnet run

Visual Studio

Visual Studio uses an inconsistent working directory when running .NET Core console apps. (see dotnet/project-system#3619) This results in an exception being thrown: no such table: Blogs. To update the working directory:

  • Right-click on the project and select Edit Project File

  • Just below the TargetFramework property, add the following:

    <StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
  • Save the file

Now you can run the app:

  • Debug > Start Without Debugging

Next steps

  • Follow the ASP.NET Core Tutorial to use EF Core in a web app
  • Learn more about LINQ query expressions
  • Configure your model to specify things like required and maximum length
  • Use Migrations to update the database schema after changing your model
You can’t perform that action at this time.