Skip to main content

Entity Framework Core

Updated 3 min readMarkdown

Extremely basic Getting Started with Entity Framework Core.

If you've never used EF Core before you need to install the tools globally.

Open the Visual Studio Terminal window and do

dotnet tool install --global dotnet-ef

If you've already installed the tools but there has been an update in the meantime, you can bring them up to date by doing

dotnet tool update --global dotnet-ef

Your project needs a couple of NuGet packages

  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Sqlite

(If you're using a different database then replace the SQLite package.)

You start off by defining some entity classes, e.g.

public class Movie {
  public int Id { get; set; }
  public required string Title { get; set; }
  public int? ReleaseYear { get; set; }
}

Create a new class that inherits from DbContext

public class MovieDatabaseDbContext : DbContext {
  public MovieDatabaseDbContext(DbContextOptions options) : base(options) { }
  public DbSet<Movie> Movies => Set<Movie>();
}

Register it in Program.cs

builder.Services.AddDbContext(options =>
  options.UseSqlite(builder.Configuration.GetConnectionString("Default")));

And add the connection string into appsettings.json

"ConnectionStrings": { "Default": "Data Source=movies.db" }

Now we're going to create a migration. That word has always bamboozled me because to me, a migration is a big project where you're changing something important, like moving your application to a different framework or a different database. But in the context of EF Core, a migration is a set of instructions for creating or updating your database schema.

Open the Visual Studio Terminal window, navigate to the folder that contains the project that contains your DbContext class, and do

dotnet ef migrations add InitialCreate

InitialCreate here can be anything you like. It's just a name that uniquely identifies this migration.

EF Core builds your project, and creates an instance of your DbContext class so that it can figure out what it needs to do to create the database.

If your DbContext isn't in an ASP.NET project (for example if you're putting your data access code in a separate class library), you need a slightly different command

dotnet ef migrations add InitialCreate --project MyDataAccessLib --startup-project MyWebApiProject

--project tells it where to look for the DbContext class

--startup-project tells it where to look for the code that instantiates the DbContext (in the Program.cs of your API project)

You can leave out --project and it'll look in the project folder that you're currently in.

If that completes successfully, you'll have a Migrations folder containing

  • [timestamp]_InitialCreate.cs
  • [timestamp]_InitialCreate.Designer.cs
  • MovieDatabaseDbContextModelSnapshot.cs

InitialCreate.cs is the migration code. It contains a method Up() to implement the changes in this migration, and a method Down() to reverse them.

You don't have to worry about InitialCreate.Designer.cs. That's just internal stuff that EF Core needs.

The snapshot is a snapshot of where the database should be when all the migrations have been applied.

To actually create the database, in the Terminal window, run

dotnet ef database update

This will create the database (movies.db) in the same folder as your project.

No comments yet. Be the first to comment.

More from this blog