I am using entity framework 4 and I have many tables (500). My edmx file is very large and I find it really hard to open it and make changes on it. I found that in my project there are "groups" of tables that related to specific business so I would like to saperate the edmx into several files. I am using repository pattern and unit of work pattern and work with POCOs this way:
This is my container
public partial class MyEntities : ObjectContext {
#region Private Methods
private void SetContextOptions() {
ContextOptions.LazyLoadingEnabled = false;
}
#endregion
#region Constructors
public MyEntities()
: base("name=MyConnection", "MyEntities") {
SetContextOptions();
OnContextCreated();
}
#endregion
#region Partial Methods
partial void OnContextCreated();
#endregion
}
For each edmx I will set the Entity Container Name property to MyEntities. This is my generic repository:
public class Repository<T> : IRepository<T> where T : class, IDataEntity
{
ObjectContext _context;
IObjectSet<T> _objectSet;
protected ObjectContext Context
{
get
{
if (_context == null)
{
_context = GetCurrentUnitOfWork<EFUnitOfWork>().Context;
}
return _context;
}
}
protected IObjectSet<T> ObjectSet
{
get
{
if (_objectSet == null)
{
_objectSet = this.Context.CreateObjectSet<T>();
}
return _objectSet;
}
}
public TUnitOfWork GetCurrentUnitOfWork<TUnitOfWork>() where TUnitOfWork : IUnitOfWork
{
return (TUnitOfWork)UnitOfWork.Current;
}
public virtual IQueryable<T> GetQuery(IEnumerable<Expression<Func<T, object>>> includes)
{
return ObjectSet.IncludeMultiple(includes);
}
public virtual IPaged<T> GetQuery(IQueryable<T> query,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy, int pageNumber, int pageSize)
{
if (orderBy != null)
{
query = orderBy(query);
}
IPaged<T> page = new Paged<T>(query, pageNumber, pageSize);
return page;
}
public virtual IPaged<T> GetQuery(IEnumerable<T> query,
Func<IEnumerable<T>, IOrderedEnumerable<T>> orderBy, int pageNumber, int pageSize)
{
if (orderBy != null)
{
query = orderBy(query);
}
IPaged<T> page = new Paged<T>(query, pageNumber, pageSize);
return page;
}
public virtual IEnumerable<T> GetObjectStateManagerChanges()
{
return this.Context.ObjectStateManager.
GetObjectStateEntries(EntityState.Added | EntityState.Modified).
Select(e => e.Entity).
OfType<T>();
}
public virtual void Insert(T entity)
{
this.ObjectSet.AddObject(entity);
}
public virtual void Delete(T entity)
{
this.ObjectSet.DeleteObject(entity);
}
public virtual void MarkModified(T entity)
{
this.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
public virtual void Attach(T entity)
{
ObjectStateEntry entry = null;
if (this.Context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry) == false)
{
this.ObjectSet.Attach(entity);
}
}
public virtual void Detach(T entity)
{
ObjectStateEntry entry = null;
if (this.Context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry) == true)
{
this.ObjectSet.Detach(entity);
}
}
public virtual T GetOriginalEntity(Func<T, bool> predicate)
{
T originalEntity = null;
EFUnitOfWorkFactory factory = new EFUnitOfWorkFactory();
using (EFUnitOfWork uow = (EFUnitOfWork)factory.Create())
{
originalEntity = uow.Context.CreateObjectSet<T>().Single(predicate);
}
return originalEntity;
}
}
And this is my unit of work implementation:
public class EFUnitOfWorkFactory : IUnitOfWorkFactory
{
private static int Counter = 0;
private static Func<ObjectContext> _objectContextDelegate;
private static readonly Object _lockObject = new object();
public static void SetObjectContext(Func<ObjectContext> objectContextDelegate)
{
_objectContextDelegate = objectContextDelegate;
}
public IUnitOfWork Create()
{
ObjectContext context;
lock (_lockObject)
{
Counter++;
context = _objectContextDelegate();
}
return new EFUnitOfWork(context, Counter);
}
}
public class EFUnitOfWork : IUnitOfWork, IDisposable
{
public ObjectContext Context { get; private set; }
public int Id { get; private set; }
public EFUnitOfWork(ObjectContext context, int id)
{
Id = id;
Context = context;
Context.ContextOptions.LazyLoadingEnabled = false;
}
public int Commit()
{
return Context.SaveChanges();
}
public void Dispose()
{
if (Context != null)
{
Context.Dispose();
Context = null;
}
GC.SuppressFinalize(this);
}
}
will my plan work to divide the edmx work with this code? What is the best practices for my case (I have read the two parts of http://blogs.msdn.com/b/adonet/archive/2008/11/24/working-with-large-models-in-entity-framework-part-1.aspx)?
EDIT: When trying to set the entity container names of the edmxs to MyEntities, I get:
EntityContainer name 'MyEntities' is already in use by another Entity Data Model in the project.
Is there any workaround?