text
stringlengths
0
13.4k
Connect to a database
Connect to a database
There are a few things you need to use Entity Framework Core to
connect to a database. Since you used dotnet new and the MVC +
Individual Auth template to set your project, you've already got them:
The Entity Framework Core packages. These are included by default
in all ASP.NET Core projects.
A database (naturally). The app.db file in the project root directory
is a small SQLite database created for you by dotnet new . SQLite is
a lightweight database engine that can run without requiring you to
install any extra tools on your machine, so it's easy and quick to use
in development.
A database context class. The database context is a C# class that
provides an entry point into the database. It's how your code will
interact with the database to read and save items. A basic context
class already exists in the Data/ApplicationDbContext.cs file.
A connection string. Whether you are connecting to a local file
database (like SQLite) or a database hosted elsewhere, you'll define
a string that contains the name or address of the database to
connect to. This is already set up for you in the appsettings.json
file: the connection string for the SQLite database is
DataSource=app.db .
Entity Framework Core uses the database context, together with the
connection string, to establish a connection to the database. You need to
tell Entity Framework Core which context, connection string, and
database provider to use in the ConfigureServices method of the
Startup class. Here's what's defined for you, thanks to the template:
services.AddDbContext<ApplicationDbContext>(options =>
49
Connect to a database
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
This code adds the ApplicationDbContext to the service container, and
tells Entity Framework Core to use the SQLite database provider, with
the connection string from configuration ( appsettings.json ).
As you can see, dotnet new creates a lot of stuff for you! The database
is set up and ready to be used. However, it doesn't have any tables for
storing to-do items. In order to store your TodoItem entities, you'll need
to update the context and migrate the database.
50
Update the context
Update the context
There's not a whole lot going on in the database context yet:
Data/ApplicationDbContext.cs
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(
DbContextOptions<ApplicationDbContext> options)
: base(options)