ObjectContextObjectContext is responsible for:
ObjectContext also encapsulates these:
ObjectStateManager to track changes to the objects.ObjectContext supports Compiled Queries and self-tracking of Entities.public class MyObjectContext : ObjectContext
{
// It uses "ObjectSet" instead of "DbSet":
public ObjectSet<Clan> Clans { get; set; }
public ObjectSet<NinjaEquipment> NinjaEquipments { get; set; }
public ObjectSet<Ninja> Ninjas { get; set; }
public MyObjectContext() : base("name=MyObjectContext")
{
Clans = CreateObjectSet<Clan>();
NinjaEquipments = CreateObjectSet<NinjaEquipment>();
Ninjas = CreateObjectSet<Ninja>();
}
// It doesn't use "OnModelCreating".
}
DbContextDbContext is the primary class that is responsible for interacting with data as object.
DBContext represents a combination of the Unit-Of-Work and Repository patterns. It does following activities:
DbContext is conceptually similar to ObjectContext.
ObjectContext.DBContext API is easier to use than ObjectContext API for all common tasks.
ObjectContext from DBContext in order to use some of the features of ObjectContext.IObjectContextAdapter:using (var db = new MyDbContext())
{
var objectContext = (context as IObjectContextAdapter).ObjectContext;
ObjectQuery<Ninja> ninjas = objectContext.CreateQuery<Ninja>(
@"SELECT VALUE n FROM NinjaContext.Ninjas AS n WHERE n.DateOfBirth > @dob",
new ObjectParameter[] { new ObjectParameter("dob", new DateTime(1984, 1, 1)) });
}
DbContext is not thread-safe.
DbContext maintains to offer its Identity Map, change tracking and Unit of Work functionalities.DbContext-derived instance from multiple threads simultaneously.DbContext in each thread.DBSetDBSet class represents an entity set that is used for the create, read, update, and delete operations.DBSet (DbSet<TEntity>) can be used when the type of entity is not known at build time.DBEntityEntryEntry method of DBContext:**`DBEntityEntry studentEntry = dbcontext.Entry(StudentEntity);`**
DbContext Lifetime