Note that there are some explanatory texts on larger screens.

plurals
  1. POentity framework - working with several edmx files
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 } </code></pre> <p>For each edmx I will set the Entity Container Name property to MyEntities. This is my generic repository:</p> <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class, IDataEntity { ObjectContext _context; IObjectSet&lt;T&gt; _objectSet; protected ObjectContext Context { get { if (_context == null) { _context = GetCurrentUnitOfWork&lt;EFUnitOfWork&gt;().Context; } return _context; } } protected IObjectSet&lt;T&gt; ObjectSet { get { if (_objectSet == null) { _objectSet = this.Context.CreateObjectSet&lt;T&gt;(); } return _objectSet; } } public TUnitOfWork GetCurrentUnitOfWork&lt;TUnitOfWork&gt;() where TUnitOfWork : IUnitOfWork { return (TUnitOfWork)UnitOfWork.Current; } public virtual IQueryable&lt;T&gt; GetQuery(IEnumerable&lt;Expression&lt;Func&lt;T, object&gt;&gt;&gt; includes) { return ObjectSet.IncludeMultiple(includes); } public virtual IPaged&lt;T&gt; GetQuery(IQueryable&lt;T&gt; query, Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy, int pageNumber, int pageSize) { if (orderBy != null) { query = orderBy(query); } IPaged&lt;T&gt; page = new Paged&lt;T&gt;(query, pageNumber, pageSize); return page; } public virtual IPaged&lt;T&gt; GetQuery(IEnumerable&lt;T&gt; query, Func&lt;IEnumerable&lt;T&gt;, IOrderedEnumerable&lt;T&gt;&gt; orderBy, int pageNumber, int pageSize) { if (orderBy != null) { query = orderBy(query); } IPaged&lt;T&gt; page = new Paged&lt;T&gt;(query, pageNumber, pageSize); return page; } public virtual IEnumerable&lt;T&gt; GetObjectStateManagerChanges() { return this.Context.ObjectStateManager. GetObjectStateEntries(EntityState.Added | EntityState.Modified). Select(e =&gt; e.Entity). OfType&lt;T&gt;(); } 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&lt;T, bool&gt; predicate) { T originalEntity = null; EFUnitOfWorkFactory factory = new EFUnitOfWorkFactory(); using (EFUnitOfWork uow = (EFUnitOfWork)factory.Create()) { originalEntity = uow.Context.CreateObjectSet&lt;T&gt;().Single(predicate); } return originalEntity; } } </code></pre> <p>And this is my unit of work implementation:</p> <pre><code> public class EFUnitOfWorkFactory : IUnitOfWorkFactory { private static int Counter = 0; private static Func&lt;ObjectContext&gt; _objectContextDelegate; private static readonly Object _lockObject = new object(); public static void SetObjectContext(Func&lt;ObjectContext&gt; 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); } } </code></pre> <p>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 <a href="http://blogs.msdn.com/b/adonet/archive/2008/11/24/working-with-large-models-in-entity-framework-part-1.aspx" rel="nofollow">http://blogs.msdn.com/b/adonet/archive/2008/11/24/working-with-large-models-in-entity-framework-part-1.aspx</a>)? </p> <p><strong>EDIT:</strong> When trying to set the entity container names of the edmxs to MyEntities, I get: </p> <pre><code>EntityContainer name 'MyEntities' is already in use by another Entity Data Model in the project. </code></pre> <p>Is there any workaround?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload