Set up Entity Framework Core with migrations and queries
✓Works with OpenClaudeYou are a .NET backend developer. The user wants to set up Entity Framework Core with migrations and implement basic queries.
What to check first
- Run
dotnet --versionto ensure .NET 6+ is installed - Verify
Microsoft.EntityFrameworkCoreandMicrosoft.EntityFrameworkCore.SqlServerNuGet packages are installed withdotnet list package
Steps
- Create a DbContext class that inherits from
DbContextand define DbSet properties for your entities - Install EF Core tools with
dotnet tool install --global dotnet-efif not already present - Create your first migration using
dotnet ef migrations add InitialCreate --project YourProject - Review the generated migration file in the
Migrationsfolder to ensure it matches your schema intent - Apply the migration to your database with
dotnet ef database update - Configure the DbContext in
Program.csusingservices.AddDbContext<YourDbContext>(options => options.UseSqlServer(connectionString)) - Implement repository methods or use DbContext directly in services to query entities with LINQ
- Test queries by injecting DbContext into your controllers or services and executing
.ToList(),.FirstOrAsync(), or.Where()chains
Code
// 1. Define your entities
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Product> Products { get; set; }
}
// 2. Create DbContext
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Category>()
.HasMany(c => c.Products)
.WithOne(p => p.Category)
.HasForeignKey(p => p.CategoryId);
}
}
// 3. Register in Program.cs
var builder = WebApplicationBuilder.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(connectionString));
// 4. Use in a service or controller
public class ProductService
{
private readonly AppDbContext _context;
public ProductService(AppDbContext context)
{
_context = context;
}
public async Task
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related .NET / C# Skills
Other Claude Code skills in the same category — free to download.
ASP.NET Core API
Scaffold ASP.NET Core Web API with controllers and middleware
ASP.NET Auth
Configure ASP.NET Identity with JWT and cookie authentication
Minimal API
Build minimal APIs with endpoint mapping
Blazor
Create Blazor components for interactive web UI
.NET Testing
Write xUnit tests with Moq and FluentAssertions
SignalR
Implement real-time communication with SignalR
.NET Background Services
Create background services with IHostedService
Want a .NET / C# skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.