Microsoft Developer Network
Entity Framework (EF) is a set of data access technologies for .NET Framework that enables developers to work with data as objects by eliminating the need for most of the data-access code that developers traditionally need to write. EF supports the Model-First, Database-First, and Code-First approaches to starting development.
Entity Framework is Microsoft's recommended Object-Relational Mapper (ORM) for the .NET platform. It allows developers to query and manipulate data in relational databases using domain-specific language (DSL) that is .NET language-specific. Developers can define their data schema and logic in C# or Visual Basic .NET, and EF generates the database and handles data persistence.
Key benefits of using EF include:
Entity Framework offers a rich set of features to facilitate data access:
Entity Framework supports three primary approaches to developing applications:
In the Code-First approach, you start by defining your domain-specific classes (entities) in C# or VB.NET. EF then infers the database schema from these classes. This approach is ideal for developers who prefer to work with code first and let EF handle database creation.
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual 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 virtual Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
With the Database-First approach, you start with an existing database. EF tools can then generate an Entity Data Model (EDM) and corresponding classes based on the database schema. This is useful when working with legacy databases.
You can use the EF Designer in Visual Studio or the EF Core Power Tools to reverse-engineer your database into model files.
The Model-First approach allows you to design your data model visually using the EF Designer in Visual Studio. EF then generates both the database schema and the C# or VB.NET classes from this visual model. This approach is good for rapid prototyping and when a visual representation of the data model is preferred.
The Entity Data Model (EDM) is a conceptual model that represents the data for your application. It consists of:
The EDM abstracts the underlying database schema, allowing you to focus on your application's data domain.
The EntityClient provider is a .NET data provider that allows you to query an EDM using Entity SQL. Entity SQL is a declarative, SQL-like language that operates on the EDM, not directly on tables. This provides a consistent way to query data regardless of the underlying database.
using System.Data.EntityClient;
using System.Data.Objects;
// ...
using (var connection = new EntityClientConnection("name=BloggingContext"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT VALUE blog FROM BloggingContext.Blogs AS blog WHERE blog.Name = 'My First Blog'";
using (var reader = new ObjectQuery<Blog>(command.CommandText, ((IObjectContextAdapter)context).ObjectContext))
{
foreach (var blog in reader)
{
Console.WriteLine(blog.Name);
}
}
}
To start using Entity Framework in your .NET project:
Microsoft.EntityFrameworkCore
for EF Core, or EntityFramework
for older versions).DbContext
: Define a class that inherits from DbContext
and includes DbSet<TEntity>
properties for your entities.Entity Framework empowers .NET developers to interact with databases in a more productive and object-oriented manner. By abstracting low-level data access details, EF allows developers to concentrate on business logic rather than plumbing. Whether you choose Code-First, Database-First, or Model-First, Entity Framework provides a robust and flexible solution for data persistence in your .NET applications.
For more detailed information, explore the following resources: