How to Connect to Database Using EF Core in C#
To connect to a database using
EF Core in C#, define a DbContext class with your entities and configure the connection string in OnConfiguring or via dependency injection. Then, use this context to query or save data to the database.Syntax
Here is the basic syntax to connect to a database using EF Core:
- DbContext class: Represents a session with the database.
- DbSet properties: Represent tables in the database.
- OnConfiguring method: Sets the database provider and connection string.
csharp
using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=your_server;Database=your_db;Trusted_Connection=True;"); } } public class Person { public int Id { get; set; } public string Name { get; set; } }
Example
This example shows how to create a DbContext, add a new record, and read records from the database.
csharp
using System; using System.Linq; using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Data Source=people.db"); } } public class Person { public int Id { get; set; } public string Name { get; set; } } class Program { static void Main() { using var context = new MyDbContext(); context.Database.EnsureCreated(); // Add a new person context.People.Add(new Person { Name = "Alice" }); context.SaveChanges(); // Read and print all people var people = context.People.ToList(); foreach (var person in people) { Console.WriteLine($"Person Id: {person.Id}, Name: {person.Name}"); } } }
Output
Person Id: 1, Name: Alice
Common Pitfalls
Common mistakes when connecting to a database using EF Core include:
- Not installing the correct EF Core database provider package (e.g.,
Microsoft.EntityFrameworkCore.SqlServerorMicrosoft.EntityFrameworkCore.Sqlite). - Forgetting to call
SaveChanges()after adding or modifying data. - Incorrect or missing connection string.
- Not creating or migrating the database before querying.
csharp
/* Wrong: Missing SaveChanges() - data won't be saved */ context.People.Add(new Person { Name = "Bob" }); // context.SaveChanges(); // This line is required /* Correct: Call SaveChanges() to persist data */ context.People.Add(new Person { Name = "Bob" }); context.SaveChanges();
Quick Reference
Summary tips for connecting to a database using EF Core:
- Define a
DbContextwithDbSetproperties for your tables. - Configure the database provider and connection string in
OnConfiguringor via dependency injection. - Use
EnsureCreated()or migrations to create the database. - Always call
SaveChanges()to save data. - Install the correct EF Core provider package for your database.
Key Takeaways
Define a DbContext class with DbSet properties to represent your database tables.
Configure the connection string and database provider in OnConfiguring or via dependency injection.
Call SaveChanges() after adding or modifying data to persist changes.
Ensure the database is created or migrated before querying data.
Install the correct EF Core provider package for your database type.