Skip to content

Guide · Open source

How to use EF Core: install, query and save data in .NET

Updated · written with AI from the project's own documentation and checked against our catalogue data

EF Core is an object-relational mapper for .NET, maintained by Microsoft and released under the MIT License. It lets you work with database data as C# objects and LINQ queries instead of writing raw SQL for most everyday tasks. This guide shows how to install it, what a basic insert, query, update and delete looks like, and when it is a poor fit.

Entity Framework Coredotnet/efcore · ★ 14.8k · MIT

Quick start

  1. 1Install the .NET SDK. EF Core needs the .NET SDK. The README does not state a required version, so check the EF Core docs for your target release.
  2. 2Pick a provider. Choose the provider package for your database, such as SqlServer, Sqlite or Cosmos.
  3. 3Install the package. Run dotnet add package with the provider name in your project folder.
  4. 4Define a DbContext and model. Create a context class and entity classes such as Blog and Post, following the getting started guide.
  5. 5Save and query data. Use db.Add, db.SaveChanges and LINQ queries such as db.Blogs.OrderBy(...).First().
  6. 6Manage schema changes. Use schema migrations from code to keep the database structure in step with your model.

What EF Core is and who it is for

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. It tracks changes to objects, saves updates, and can manage schema migrations from code.

It suits C# developers building .NET applications on a relational or document database, and teams that prefer LINQ over hand-written SQL for everyday data access. It is a code library, so it is not a no-code tool or a hosted database service.

  • Good fit: .NET apps that use SQL Server, SQLite, PostgreSQL, MySQL or Cosmos DB through provider packages
  • Good fit: projects that want schema migrations managed from code
  • Poor fit: non-.NET developers, or projects that need full control over hand-tuned SQL

Install EF Core and a database provider

EF Core is available on NuGet. Install the provider package that matches your target database. The README lists the SQL Server, SQLite and Cosmos providers as examples. The prerequisite is the .NET SDK; the README does not specify a version.

For a database not listed in the README, check the list of providers in the EF Core docs. To install a preview version, use the --version option on the same command.

dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Sqlite dotnet add package Microsoft.EntityFrameworkCore.Cosmos

Use SQLite with Microsoft.Data.Sqlite

For a local SQLite database in a .NET app, install the SQLite provider. Microsoft.Data.Sqlite is a lightweight ADO.NET provider for SQLite, and the EF Core SQLite provider is built on top of it. It can also be used on its own or with other data access libraries.

dotnet add package Microsoft.Data.Sqlite

Run basic insert, query, update and delete operations

The README's basic example uses a BloggingContext class. Adding an object and calling SaveChanges writes it to the database. Querying uses LINQ, updating changes tracked objects before SaveChanges, and deleting uses Remove followed by SaveChanges.

The README points to the getting started page in the docs for a full walkthrough that configures the DbContext, defines the model and creates the database.

  • Add: db.Add(...) then db.SaveChanges()
  • Query: db.Blogs.OrderBy(...).First()
  • Update: change properties on a tracked object, then db.SaveChanges()
  • Delete: db.Remove(...) then db.SaveChanges()
using var db = new BloggingContext(); // Inserting data into the database db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); db.SaveChanges(); // Querying var blog = db.Blogs .OrderBy(b => b.BlogId) .First(); // Updating blog.Url = "https://devblogs.microsoft.com/dotnet"; blog.Posts.Add( new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); db.SaveChanges(); // Deleting db.Remove(blog); db.SaveChanges();

Get support and read the source

For questions about using the projects, the README suggests asking on Stack Overflow under the entity-framework-core tag. To report a bug or request a feature, submit an issue on the dotnet/efcore GitHub repository. To build from source instead of using NuGet packages, see the build guide in the repository docs.

What people use it for

  • Storing blog posts and their comments in a SQL Server database from a C# web app
  • Reading and updating rows in a local SQLite file from a .NET desktop or console tool
  • Querying a relational database with LINQ instead of writing SQL strings
  • Keeping database schema changes tracked in code alongside the application

Frequently asked questions

Is EF Core free to use?

Yes. The repository is licensed under the MIT License, which the README links to. Check the LICENSE file in the repository for the full terms.

What do I need before installing EF Core?

You need the .NET SDK, and the EF Core packages are installed from NuGet using dotnet add package. The README does not specify a particular SDK version, so check the docs for your target release.

Which databases does EF Core support?

The README says it works with SQL Server, Azure SQL Database, SQLite, Azure Cosmos DB, MariaDB, MySQL, PostgreSQL and other databases through a provider plugin API. Install the matching provider package for your database.

How do I use EF Core with SQLite?

Install the Microsoft.EntityFrameworkCore.Sqlite package. The EF Core SQLite provider is built on Microsoft.Data.Sqlite, which you can also install on its own with dotnet add package Microsoft.Data.Sqlite.

Do I still need to write SQL with EF Core?

The source says EF Core lets you avoid writing raw SQL for most tasks, using LINQ queries. Projects that need hand-tuned SQL may prefer a lighter tool.

How does EF Core compare to Dapper or NHibernate?

The README does not compare them. Dapper, NHibernate and LinqToDB are commonly mentioned alternatives. EF Core focuses on LINQ, change tracking and migrations, while a mapper with less abstraction may suit projects that want to control SQL directly.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

EF Core suits C# developers building .NET apps who want LINQ queries, change tracking and code-managed migrations over a relational or document database. If you are not using .NET, want a no-code tool or need hand-tuned SQL with minimal abstraction, pick a different tool.

Install options & downloads