Entity Framework API Reference
Namespaces
This section provides a detailed reference for the core namespaces within the Entity Framework library. Understanding these namespaces is crucial for effectively using Entity Framework in your .NET applications.
System.Data.Entity
This namespace contains the fundamental types and classes for Entity Framework, including DbContext
, DbSet<TEntity>
, and configuration classes.
System.Data.Entity.Migrations
Provides types for managing database schema migrations, including automatic migrations and custom migration operations.
System.Data.Entity.Infrastructure
Contains types related to the infrastructure of Entity Framework, such as database initialization, connection handling, and entity states.
Classes
Explore the key classes that form the backbone of Entity Framework's functionality.
DbContext
The primary class for interacting with the Entity Framework. It represents a session with the database and allows you to query and save data.
public class DbContext : IDisposable
- Constructors
-
public DbContext()
public DbContext(string nameOrConnectionString)
public DbContext(DbConnection existingConnection, bool contextOwnsConnection)
- Properties
-
Database
: Gets access to the underlying database. -
Set<TEntity>()
: Gets aDbSet<TEntity>
representing the set of entities of the given type. - Methods
-
SaveChanges()
: Saves all changes made in this context to the underlying database. -
Dispose()
: Releases the unmanaged resources that are used by the object.
DbSet<TEntity>
Represents a collection of all entities in the context, or that can be queried from a given database.
public class DbSet<TEntity> : IDbSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IEnumerable
- Properties
-
EntityType
: Gets the metadata for the entity type. - Methods
-
Add(TEntity entity)
: Adds a new entity to the context. -
Remove(TEntity entity)
: Removes an entity from the context. -
Find(params object[] keyValues)
: Finds an entity with the given primary key values.
DbModelBuilder
Used to set up conventions, build mappings, and configure the model for Entity Framework.
public class DbModelBuilder
- Methods
-
Entity<T>()
: Configures aT
entity. -
Conventions
: Gets a collection of conventions that will be used to apply mappings.
Interfaces
Key interfaces that define contracts for Entity Framework operations and types.
IDbContextFactory<TContext>
An interface for creating instances of DbContext
.
public interface IDbContextFactory<TContext> where TContext : DbContext
- Methods
-
Create()
: Creates a new instance of the context.
IDbSet<TEntity>
Represents a collection of entities. Implemented by DbSet<TEntity>
.
public interface IDbSet<TEntity> : IQueryable<TEntity>, IEnumerable<TEntity>, IEnumerable where TEntity : class
- Methods
-
Add(TEntity entity)
-
Remove(TEntity entity)
-
Find(params object[] keyValues)
Enumerations
Enumerated types used within Entity Framework.
EntityState
Specifies the current state of an entity in the context.
public enum EntityState
- Values
: The entity is new and has been added to the context.Added
: The entity's properties have been modified.Modified
: The entity has been removed from the context.Deleted
: The entity has not been modified.Unchanged
: The entity is not currently being tracked by the context.Detached