我正在使用实体框架 4,并且我有很多表(500)。我的 edmx 文件非常大,我发现很难打开它并对其进行更改。我发现在我的项目中有与特定业务相关的表“组”,所以我想将 edmx 分成几个文件。我正在使用存储库模式和工作单元模式,并以这种方式使用 POCO:
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
}
对于每个 edmx,我将 Entity Container Name 属性设置为 MyEntities。这是我的通用存储库:
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;
}
}
这是我的工作实施单元:
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);
}
}
我的计划是否可以使用此代码划分 edmx 工作?我的案例的最佳做法是什么(我已阅读http://blogs.msdn.com/b/adonet/archive/2008/11/24/working-with-large-models-in-entity-的两部分框架部分 1.aspx)?
编辑:当尝试将 edmxs 的实体容器名称设置为 MyEntities 时,我得到:
EntityContainer name 'MyEntities' is already in use by another Entity Data Model in the project.
有什么解决方法吗?