ADO.NET Documentation
Entity Framework (EF) is a set of technologies in ADO.NET that supports the development of data-access applications by using an object-relational mapping (ORM) approach. EF allows developers to work with data in the form of domain-specific objects and properties, such as objects of business classes, instead of the underlying database tables and columns that they are usually persisted in. This abstraction simplifies data access and makes applications more maintainable.
Modeling is a core aspect of using Entity Framework. It involves defining how your application's domain objects map to the database schema. This mapping can be achieved in several ways, each with its own advantages:
An entity represents a real-world object that has an identity. In EF, entities are typically represented by CLR classes. An entity type is the definition of an entity, including its properties and relationships. Each entity instance has a unique entity key that distinguishes it from other entities of the same type.
An entity set is a collection of entities of a specific entity type. In a database, an entity set typically maps to a table. When you query your entities, you query their respective entity sets.
Entities can have relationships with other entities. These relationships can be one-to-one, one-to-many, or many-to-many. EF models these relationships using navigation properties, which allow you to navigate from one related entity to another.
Consider a Blog
entity and a collection of Post
entities associated with it. The Blog
entity might have a navigation property named Posts
that is a collection of Post
objects, and each Post
entity might have a navigation property named Blog
that refers to its parent Blog
.
A complex type is a user-defined type that does not have an entity key. It's essentially a grouping of properties that are part of another entity. For example, an Address
can be a complex type with properties like Street
, City
, and ZipCode
, which would be part of a Customer
entity.
This is a popular approach where you define your domain models as POCO (Plain Old CLR Object) classes. Entity Framework inspects these classes to infer the mapping to the database. Conventions are heavily used, but you can also use data annotations or the fluent API to customize the mapping.
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; }
}
If you have an existing database, you can use the EF Designer (in Visual Studio) to reverse-engineer the database schema and generate entity classes and a DbContext
. This is useful for integrating EF into legacy systems.
In this approach, you start by visually designing your data model using the Entity Designer. You define entities, their properties, and relationships. From this model, you can generate classes and scripts to create the database schema.
The DbContext
class is the gateway to interacting with your data in Entity Framework. It represents a session with the database and allows you to query and save data. When you use Code First, you typically create a derived DbContext
class and define DbSet<TEntity>
properties for each of your entity types.
using Microsoft.EntityFrameworkCore;
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;");
}
}