C# > Data Access > Database Access > Entity Framework Core Basics
Creating a DbContext in Entity Framework Core
This snippet demonstrates how to create a DbContext
in Entity Framework Core, which acts as a session to the database and provides access to your entities. The DbContext is the central class for interacting with the database.
Code Example
This code defines a DbContext
class named BloggingContext
. It inherits from DbContext
and includes two DbSet
properties: Blogs
and Posts
. These DbSet
properties allow you to query and save instances of the Blog
and Post
entities. The constructor accepts DbContextOptions
, which allows you to configure the database provider (e.g., SQL Server, SQLite) and connection string. The OnModelCreating
method is overridden to configure relationships and constraints using the Fluent API. The commented-out example shows how to define a one-to-many relationship between Post
and Blog
.
using Microsoft.EntityFrameworkCore;
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options)
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure relationships and constraints here using Fluent API
// Example: modelBuilder.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts).HasForeignKey(p => p.BlogId);
}
}
Concepts Behind the Snippet
The DbContext
is the core of EF Core. It represents a connection to the database and provides methods for querying, adding, updating, and deleting entities. The DbSet
properties represent tables in the database. The OnModelCreating
method allows you to further customize the model using the Fluent API, which provides more fine-grained control over the database schema.
Real-Life Use Case
In the blogging platform example, the BloggingContext
would be used to interact with the database containing blog posts. You would use the Blogs
and Posts
DbSet
properties to retrieve, add, update, and delete blog posts. The OnModelCreating
method would be used to configure relationships between blogs and posts, ensuring data integrity.
Best Practices
DbContext
instances when you're finished with them, especially in web applications. Dependency Injection is a good way to manage the lifetime of your DbContext
.OnModelCreating
method to configure complex relationships and constraints that cannot be easily expressed using Data Annotations.DbContext
to prevent blocking the database connection.
Interview Tip
Be prepared to explain the role of the DbContext
in EF Core. Understand how to configure the database connection using DbContextOptions
. Also, be familiar with the DbSet
properties and the OnModelCreating
method.
When to Use Them
You must create a DbContext
whenever you want to use EF Core to interact with a database. It is the primary entry point for all database operations. The DbContext
should be scoped to the operation being performed to manage resources effectively.
FAQ
-
How do I configure the database connection string?
You can configure the connection string in theConfigureServices
method of yourStartup.cs
file (in ASP.NET Core) or in your application's configuration file (e.g., appsettings.json). You would then pass the connection string to theDbContextOptionsBuilder
when configuring theDbContext
. -
What database providers does EF Core support?
EF Core supports a wide range of database providers, including SQL Server, SQLite, PostgreSQL, MySQL, and Cosmos DB. You need to install the appropriate NuGet package for the database provider you want to use.