13. Database Integration with .NET
In modern software development, integrating a database into your .NET applications is essential for storing and managing data efficiently. Entity Framework Core (EF Core) simplifies this process by providing a powerful ORM (Object-Relational Mapping) framework for .NET developers. In this blog, we’ll introduce Entity Framework Core, guide you through setting up a database connection, and demonstrate how to perform CRUD (Create, Read, Update, Delete) operations in a .NET application.
Introduction to Entity Framework Core
Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of the popular Entity Framework ORM framework. It enables developers to work with databases using familiar object-oriented programming techniques, allowing you to define your database schema using C# classes.
Key features of Entity Framework Core include:
- Model-First Development: Define your database schema using C# classes called entity types. These entity types represent database tables and their relationships.
- LINQ Support: Write queries against your database using Language-Integrated Query (LINQ), which allows you to query and manipulate data using C# or VB.NET syntax.
- Migration Support: Update your database schema as your application evolves using EF Core’s migration feature. Migrations enable you to make changes to your database schema in a controlled and repeatable manner.
Setting Up a Database Connection
To set up a database connection in a .NET application using Entity Framework Core, follow these steps:
- Install Entity Framework Core: Install the Entity Framework Core package via NuGet Package Manager or the .NET CLI:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Replace Microsoft.EntityFrameworkCore.SqlServer
with the appropriate provider for your database if you're using a different database engine (e.g., MySQL, PostgreSQL).
2. Define Your Data Model: Define your data model by creating C# classes that represent database tables. These classes are called entity types.
3. Create a DbContext: Create a class that derives from DbContext
, which represents a session with the database. This class contains properties that represent DbSet
objects, which correspond to database tables.
4. Configure the Database Connection: Configure the database connection string in your application’s configuration file (e.g., appsettings.json
, app.config
, web.config
).
5. Perform Database Migrations: Use Entity Framework Core’s migration feature to create or update the database schema based on your data model.
Performing CRUD Operations
Once you’ve set up the database connection and defined your data model, you can perform CRUD operations using Entity Framework Core.
Example:
Let’s assume we have a simple TodoItem
entity representing tasks in a to-do list. Here's how you can perform CRUD operations on the TodoItem
entity using Entity Framework Core:
using Microsoft.EntityFrameworkCore;
public class TodoItem
{
public int Id { get; set; }
public string Title { get; set; }
public bool IsCompleted { get; set; }
}
public class TodoDbContext : DbContext
{
public DbSet<TodoItem> TodoItems { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("your-connection-string-here");
}
}
public class Program
{
public static void Main(string[] args)
{
using (var context = new TodoDbContext())
{
// Create
var todoItem = new TodoItem { Title = "Buy groceries", IsCompleted = false };
context.TodoItems.Add(todoItem);
context.SaveChanges();
// Read
var todoItems = context.TodoItems.ToList();
// Update
var firstTodoItem = todoItems.FirstOrDefault();
if (firstTodoItem != null)
{
firstTodoItem.IsCompleted = true;
context.SaveChanges();
}
// Delete
var todoItemToDelete = context.TodoItems.FirstOrDefault();
if (todoItemToDelete != null)
{
context.TodoItems.Remove(todoItemToDelete);
context.SaveChanges();
}
}
}
}
Conclusion
Entity Framework Core simplifies database integration in .NET applications by providing a powerful ORM framework. By following the steps outlined in this blog, you can set up a database connection, define your data model, and perform CRUD operations efficiently. In the next blog, we’ll explore more advanced topics, such as querying data using LINQ and working with relationships in Entity Framework Core. Stay tuned!
More from this Series
This blog is part of my series “Building Dynamic Web Apps: React and .NET Unleashed”. If you found this helpful, be sure to check out the other posts in the series: