title: Exposing EntityFramework 4 entities as IList instead of IObjectSet
body: <p>I have a 'Customer' POCO entity within my Entity Framework 4 project. I want to expose my Customer entities to my upper layers as a generic list rather than an ObjectSet.</p>
  
  <p>I have an IUnitOfWork interface which looks as follows:</p>
  
  <pre><code>public interface IUnitOfWork
  {
     string Save();
     IList&lt;Customer&gt; Customers { get; }
  }
  </code></pre>
  
  <p>Down at my Entity Framework DAL (which implements the above interface) I have the following:</p>
  
  <pre><code>public class EntityContainer : ObjectContext, IUnitOfWork
  {
      private IObjectSet&lt;Customer&gt; _customers;
  
      public IList&lt;Customer&gt; Customers 
      {
         get
         {
           if (_customers == null)
           {
               _customers = CreateObjectSet&lt;Customer&gt;("Customers");                    
           }
           return _customers.ToList&lt;Customer&gt;() ;
         }            
      }
  }
  </code></pre>
  
  <p>However the 'CreateObjectSet("Customers")' line doesn't work. Every time I try to add a new 'Customer' nothing happens. Interestingly, if I revert to using an IObjectSet then the code works. For example:</p>
  
  <pre><code>public interface IUnitOfWork
  {
      string Save();
      IObjectSet&lt;Contact&gt; Contacts { get; }
  }
  
  
  public class EntityContainer : ObjectContext, IUnitOfWork
  {
      private IObjectSet&lt;Customer&gt; _customers;
  
      public IObjectSet&lt;Customer&gt; Customers 
      {
         get
         {
           if (_customers == null)
           {
               _customers = CreateObjectSet&lt;Customer&gt;("Customers");                    
           }
           return _customers;
         }            
      }
  }
  </code></pre>
  
  <p>IQueryable also works, but I cannot get IList to work and I have no idea why. Anyone any ideas?</p>
  
  <h1>#</h1>
  
  <p>A correction to the original question. Using IQueryable doesn't work, nor does IEnumerable. This is because the Customer repository needs to provide 'Add' and 'Delete' methods to add/delete from the entity collection (add or remove customer entities in the above example). Neither IQueryable or IEnumerable allow you to add or remove objects; instead, an ICollection or IList must be used. This leaves me back at my original problem. I do not want to expose my collection to the repository as an ObjectSet. I want to use a type which is not tied to the EntityFramework i.e. - I want to use a generic list. </p>
  
  <p>Has anyone any more suggestions? I suspect there's a straightforward way of doing this, but I'm not familiar enough with the framework to figure it out. </p>