Understanding entity framework core
EF Core, an open-source ORM framework, facilitates the development of data-centric applications by mapping domain objects to corresponding database tables. It abstracts the complexities of database interactions, allowing developers to focus on application logic rather than SQL queries.
Key features of EF core
- Modeling entities: Developers define entity classes representing database tables and their relationships, enabling EF Core to generate database schemas automatically.
- Querying: EF Core supports LINQ (Language Integrated Query), enabling developers to construct queries using familiar C# syntax, which are then translated into SQL queries by EF Core.
- Change tracking: EF Core monitors changes to tracked entities, simplifying database updates by automatically generating necessary SQL commands during save operations.
- Database migrations: With EF Core, developers can manage database schema changes using migrations, ensuring seamless updates without manual intervention.
- Database-first approach: In the database-first approach, developers start by designing the database schema using tools like SQL Server Management Studio or Visual Studio Database Projects. EF Core then generates entity classes based on the existing database schema.
// Install EF Core tools:
dotnet tool install --global dotnet-ef
// Scaffold entities from an existing database:
dotnet ef dbcontext scaffold "connection_string" Microsoft.EntityFrameworkCore.SqlServer -o Models
- Code-first approach: Alternatively, in the code-first approach, developers define entity classes first and then generate the corresponding database schema.
// Define entity classes:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Define DbContext:
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection_string");
}
}
- Real-time implementation: Let's consider a scenario where we want to build an e-commerce application using EF Core for data access. We'll demonstrate a code-first approach to define entities and interact with the database.
Step 1. Define Entity Classes
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection_string");
}
}
Step 2. Create a Database and Seed Data.
using (var context = new AppDbContext())
{
context.Database.EnsureCreated();
if (!context.Products.Any())
{
context.Products.AddRange(
new Product { Name = "Laptop", Price = 999.99m },
new Product { Name = "Smartphone", Price = 599.99m }
);
context.SaveChanges();
}
}
Step 3. Query Data.
using (var context = new AppDbContext())
{
var products = context.Products.ToList();
foreach (var product in products)
{
Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
}
}
Conclusion
Entity Framework Core offers a streamlined approach to interacting with databases, abstracting the intricacies of data access and manipulation. Whether adopting a database-first or code-first approach, EF Core empowers developers to build robust, data-driven applications efficiently.
For a more in-depth understanding, refer to the official documentation.
By embracing EF Core, developers can enhance the productivity, maintainability, and scalability of their .NET applications, while ensuring seamless integration with various database systems.