Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of Entity Framework. It's an Object-Relational Mapper (ORM) that allows you to work with a database using .NET objects. You can use EF Core to query and save data in your database effortlessly, focusing on your domain model instead of the underlying database structure.
One of the standout features of EF Core is its database migrations, which allow you to evolve your database schema over time without losing existing data. This is crucial in development and production environments, as changing databases manually can lead to errors and data loss.
Migrations are a way to introduce changes to your database schema using code rather than manually creating and altering tables. When you modify your model classes, you can create a migration that captures those changes. EF Core will generate the SQL required to update your database from one version to the next.
To use EF Core migrations, you need to have an existing EF Core setup. Below is a simple walkthrough of creating a new project with EF Core and applying migrations.
If you don’t have a .NET project, you can create one using the command line:
dotnet new console -n MySampleApp cd MySampleApp dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools
Let’s create a simple model representing a Product
entity:
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
Next, you’ll set up a DbContext
to manage your entities. Create a new class AppDbContext
.
using Microsoft.EntityFrameworkCore; public class AppDbContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("YourConnectionStringHere"); } }
Now that your model and DB context are set up, you can create your first migration! Use the following CLI command:
dotnet ef migrations add InitialCreate
This command will create a migration file, in which EF Core tracks changes made to your model. You will see a new folder called Migrations
containing a class file representing this migration.
Once the migration is created, you can apply it to your database with:
dotnet ef database update
This command will run the generated SQL to create the necessary tables based on your Product
entity.
Let’s say you want to add a new field, StockQuantity
, to your Product
entity:
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int StockQuantity { get; set; } // New field added }
After modifying the model, you will need to create a new migration:
dotnet ef migrations add AddStockQuantity
Again, the migration file will reflect the changes made to your model.
Now, simply run:
dotnet ef database update
This will apply the new migration and update your database schema accordingly.
With EF Core migrations, you can handle complex database structures confidently, as it tracks changes in your code and helps you apply these changes in an orderly fashion.
Remember, a well-defined migration strategy is essential for maintaining a healthy database schema as your applications evolve!
Entity Framework Core and its migration capabilities make it easier to manage database changes without the typical hassle that comes with manual updates. As you deepen your understanding of migrations in EF Core, you'll find it becomes an invaluable part of your development workflow.
09/10/2024 | DotNet
12/10/2024 | DotNet
19/09/2024 | DotNet
09/10/2024 | DotNet
03/09/2024 | DotNet
19/09/2024 | DotNet
09/10/2024 | DotNet
09/10/2024 | DotNet
19/09/2024 | DotNet
09/10/2024 | DotNet