---
title: "How do you configure a DbContext in .NET Core?"  
description: "How do you configure a DbContext in .NET Core?"  
author: "Utpal Vishwas"  
published: 2025-05-20  
updated: 2025-05-20  
canonical: https://www.mindstick.com/interview/34145/how-do-you-configure-a-dbcontext-in-dot-net-core  
category: "c#"  
tags: ["c#", "api(s)", ".net core"]  
reading_time: 3 minutes  

---

# How do you configure a DbContext in .NET Core?

In **ASP.NET Core**, you configure a `DbContext` by:

1. **Creating a class that inherits from** `DbContext`
2. **Registering it in the** `Program.cs` **(or** `Startup.cs` **for older versions)**
3. **Specifying a connection string and provider (e.g., SQL Server)**

## Step-by-Step: Configuring a `DbContext`

### 1. Create a Model

```cs
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}
```

### 2. Create the DbContext

```cs
using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }

    public DbSet<Product> Products { get; set; }
}
```

### 3. Add Connection String in `appsettings.json`

```plaintext
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=MyDb;Trusted_Connection=True;"
  }
}
```

### 4. Register DbContext in `Program.cs` (.NET 6/7/8)

```cs
var builder = WebApplication.CreateBuilder(args);

// Register DbContext with SQL Server
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();

// ... app.UseRouting(), app.MapControllers(), etc.
app.Run();
```

> For other databases:
>
> Use `.UseSqlite()`, `.UseNpgsql()`, `.UseMySql()`, etc.

### Done! You can now inject `AppDbContext` anywhere:

```cs
public class ProductService
{
    private readonly AppDbContext _context;

    public ProductService(AppDbContext context)
    {
        _context = context;
    }

    public List<Product> GetAll() => _context.Products.ToList();
}
```

## Summary

| Task | How to do it |
| --- | --- |
| Define the context | Inherit from `DbContext` |
| Register in DI | `builder.Services.AddDbContext` |
| Set the connection string | In `appsettings.json` |
| Use in app | Inject via constructor |

## Answers

### Answer by Utpal Vishwas

In **ASP.NET Core**, you configure a `DbContext` by:

1. **Creating a class that inherits from** `DbContext`
2. **Registering it in the** `Program.cs` **(or** `Startup.cs` **for older versions)**
3. **Specifying a connection string and provider (e.g., SQL Server)**

## Step-by-Step: Configuring a `DbContext`

### 1. Create a Model

```cs
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}
```

### 2. Create the DbContext

```cs
using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }

    public DbSet<Product> Products { get; set; }
}
```

### 3. Add Connection String in `appsettings.json`

```plaintext
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=MyDb;Trusted_Connection=True;"
  }
}
```

### 4. Register DbContext in `Program.cs` (.NET 6/7/8)

```cs
var builder = WebApplication.CreateBuilder(args);

// Register DbContext with SQL Server
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();

// ... app.UseRouting(), app.MapControllers(), etc.
app.Run();
```

> For other databases:
>
> Use `.UseSqlite()`, `.UseNpgsql()`, `.UseMySql()`, etc.

### Done! You can now inject `AppDbContext` anywhere:

```cs
public class ProductService
{
    private readonly AppDbContext _context;

    public ProductService(AppDbContext context)
    {
        _context = context;
    }

    public List<Product> GetAll() => _context.Products.ToList();
}
```

## Summary

| Task | How to do it |
| --- | --- |
| Define the context | Inherit from `DbContext` |
| Register in DI | `builder.Services.AddDbContext` |
| Set the connection string | In `appsettings.json` |
| Use in app | Inject via constructor |


---

Original Source: https://www.mindstick.com/interview/34145/how-do-you-configure-a-dbcontext-in-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
