title: EntityFramework and ReadOnlyCollection
body: <p>I use EntityFramewotk and code first approach. So, I describe my model like this:</p>
  
  <pre><code>class Person
  {
      public long Id { get;set; }
      public string Name { get;set; }
      public ICollection&lt;Person&gt; Parents { get;set; }
  }
  </code></pre>
  
  <p>But, my domain logic don't allow to modify Parents collection (add, delete), it must be readonly (just for example). EntityFramework requires all Collections have <code>ICollection&lt;T&gt;</code> interface, and it has <code>Add</code> method (to materialize results) and <code>Remove</code> method, and others.
  I can create my own collection with explicit implementation of interface:</p>
  
  <pre><code>public class ParentsCollection : ICollection&lt;Person&gt;
  {
      private readonly HashSet&lt;Person&gt; _collection = new HashSet&lt;Person&gt;();
      void ICollection&lt;Person&gt;.Add(Person item)
      {
          _collection.Add(item);
      }
  
      bool ICollection&lt;Person&gt;.Remove(Person item)
      {
          return _collection.Remove(item);
      }
  
      //...and others
  }
  </code></pre>
  
  <p>This hides <code>Add</code> and <code>Remove</code> methods, but does not protect at all. Because I can always cast to ICollection and call prohibited method.</p>
  
  <p>So, my question is: </p>
  
  <ul>
  <li>Is there a way to work with read-only collections in EntityFramework?</li>
  </ul>