11using System.Data.Entity;
12using System.Data.Entity.Infrastructure;
13using System.Data.Entity.Core.Objects;
38 private static object _lock =
new object();
40 private string _label;
41 private string ContextLabel {
get;
set; }
99 var contextLabel = GetContextName(database, label);
101 if (ApplicationContext.LocalContext.Contains(contextLabel))
108 mgr.ContextLabel = contextLabel;
109 ApplicationContext.LocalContext[contextLabel] = mgr;
140 var contextLabel = GetContextName(context.DefaultContainerName, label);
142 if (ApplicationContext.LocalContext.Contains(contextLabel))
149 mgr.ContextLabel = contextLabel;
150 ApplicationContext.LocalContext[contextLabel] = mgr;
157 private DbContextManager(
string database,
string label, DbCompiledModel model, ObjectContext context)
163 if (!
string.IsNullOrEmpty(database))
164 _context = (C)(Activator.CreateInstance(typeof(C), database, model));
166 _context = (C) (Activator.CreateInstance(typeof (C), model));
168 else if (context !=
null)
170 _context = (C) (Activator.CreateInstance(typeof (C), context,
true));
172 else if (
string.IsNullOrEmpty(database))
174 _context = (C) (Activator.CreateInstance(typeof (C)));
178 _context = (C)(Activator.CreateInstance(typeof(C), database));
182 private static string GetContextName(
string database,
string label)
184 return "__dbctx:" + label +
"-" + database;
199 #region Reference counting
201 private int _refCount;
209 get {
return _refCount; }
212 private void AddRef()
226 ApplicationContext.LocalContext.Remove(ContextLabel);
Provides an automated way to reuse Entity Framework DbContext objects within the context of a single ...
static DbContextManager< C > GetManager(ObjectContext context)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(string database, DbCompiledModel model)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(string database, string label)
Gets the ObjectContextManager object for the specified database.
static DbContextManager< C > GetManager(ObjectContext context, string label)
Gets the ObjectContextManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static DbContextManager< C > GetManager()
Gets the ObjectContextManager object for the /// specified database.
static DbContextManager< C > GetManager(string database)
Gets the ObjectContextManager object for the specified database.
C DbContext
Gets the DbContext object.
static DbContextManager< C > GetManager(string database, string label, DbCompiledModel model)
Gets the ObjectContextManager object for the specified database.
void Dispose()
Dispose object, dereferencing or disposing the context it is managing.