Entity Framework (EF) is an Object-Relational Mapper (ORM) developed by Microsoft. It simplifies data access in applications by allowing developers to interact with databases using objects and LINQ queries instead of writing raw SQL queries. Entity Framework is a part of the larger .NET Framework, and it has evolved through various versions, the latest being Entity Framework Core (EF Core).
In this article, we will explore the key concepts of Entity Framework, its architecture, and how to use it in a typical .NET application.
Entity Framework can be broken down into three main components:
DbContext: Represents a session with the database and is used to query and save instances of the entity classes to the database. It provides a high-level API to perform CRUD operations on the database.
Entity Data Model (EDM): Represents the structure of your domain model, including entities, their properties, and relationships between entities. It is a conceptual model that maps the object-oriented domain model to the relational database schema.
LINQ Provider: Translates LINQ queries written in C# or VB.NET to SQL queries that can be executed against the database.
The architecture of Entity Framework can be divided into two layers:
Runtime Components: This layer deals with the actual interaction between your application and the database. It includes the DbContext, EDM, and LINQ Provider.
Design-time Components: This layer deals with the creation and maintenance of the EDM. It includes tools to create and update the EDM from an existing database, generate the database schema from the EDM, and validate the EDM.
The two layers work together to provide a seamless experience for developers working with data in .NET applications.
To demonstrate how to use Entity Framework in a .NET application, let’s create a simple console application.
First, we need to install the Entity Framework NuGet package. To do this, open the Package Manager Console in Visual Studio and run the following command:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Next, we will create our domain model classes. In this example, we will create a simple model for a blog that has a collection of posts:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Now, we need to create a class that inherits from DbContext
. This class will be used to interact with the database:
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
}
}
Finally, we can use the BloggingContext
class to query and save data:
static void Main(string[] args)
{
using (var context = new BloggingContext())
{
// Create a new blog and save it to the database
var blog = new Blog { Url = "https://example.com" };
context.Blogs.Add(blog);
context.SaveChanges();
// Query the database for all blogs and display them
var blogs = context.Blogs.ToList();
foreach (var b in blogs)
{
Console.WriteLine($"Blog ID: {b.BlogId}, URL: {b.Url}");
}
}
Console.ReadKey();
}
Entity Framework is a powerful ORM that simplifies data access in .NET applications. By using it, developers can focus on writing clean, maintainable code without worrying about the intricacies of raw SQL queries. With Entity Framework, you can interact with databases using familiar C# objects and LINQ queries, making it a popular choice for many developers in the .NET ecosystem.