Entity Framework (EF) Core is a modern Object-Relational Mapper (ORM) for ASP.NET Core. It allows developers to interact with databases using C# objects instead of writing raw SQL queries. EF Core supports LINQ queries, migrations, and database management, making data handling easier and more efficient.
Key Features:
- Code-first or database-first approach
- Supports LINQ queries for data manipulation
- Automatic database migrations
- Cross-platform support
- Integration with ASP.NET Core apps
Example:
A simple model and DbContext example:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
Adding a student to the database:
using(var context = new AppDbContext())
{
context.Students.Add(new Student { Name = "John Doe" });
context.SaveChanges();
}
EF Core is widely used in ASP.NET Core apps to simplify database operations and maintain clean, testable code.
Citations: