title: making a global filter for entity framework
body: <p>For my models I have a <code>active</code> attribute on all of them, and i want to filter all inactive if the model was not displayed on the administration
  What is the best way to do that, What I'm currently using is the following </p>
  
  <p>in my base model class i have this method that filters the collections</p>
  
  <pre><code>public virtual IQueryable&lt;T&gt; GlobalDefaultScope&lt;T&gt;(IQueryable&lt;T&gt; c) where T : CModel&lt;T&gt;
  {
      if (settings.is_admin)
      {
          c = c.Where(m =&gt; m.active);
      }
      return c;
  }
  </code></pre>
  
  <p>and on my model for every relation i did the following method</p>
  
  <pre><code>DbSet&lt;T&gt; set ...
  var X = set.Where(some filter);
  var list = globalDefaultScope(X).ToList();
  return list;
  </code></pre>
  
  <p>And now I'm having some serious problems when i want to eagerly load some subrelations using <code>include("Xmodel.Ymodel")</code> i called the <code>globalDefaultScope</code> in the <code>get</code> method for that collection that filters the collection but it keeps throwing this exception when some items in the collection are inactive</p>
  
  <blockquote>
    <p><strong>System.InvalidOperationException:</strong> 
      The operation failed: The relationship could not be changed because one or more 
      of the foreign-key properties is non-nullable.</p>
  </blockquote>
  
  <p>how can i fix this or how can i make this filter in a more elegant way because i'm really not very satisfied of how i implemented it.</p>
  
  <p>please ask for Any missing information or code blocks or any details </p>
  
  <p><strong>Update:</strong></p>
  
  <p>I also found this <a href="http://www.matthidinger.com/archive/2012/01/25/a-smarter-infrastructure-automatically-filtering-an-ef-4-1-dbset.aspx">link</a>, but this way didn't work with eagerly loaded entries (<code>include()</code>)</p>
  
  <p><strong>Update2:</strong></p>
  
  <p>this is an example of how i use the include and where the error occurs </p>
  
  <p><em>In My Model</em></p>
  
  <pre><code>public IQueryable&lt;Dish&gt; getSomeRelation(bool eagerly_load_sub_relation1, bool eagerly_load_sub_relation2)
          {
              var query = getQuery(...);
              //getQuery =&gt;  query = db.Entry(obj).Collection(collection).Query() 
              //GlobalDefaultScope(query)
              if ( eagerly_load_sub_relation1){
                  query = query.Include(m =&gt; m.sub_relation1);
              }
              if (eagerly_load_sub_relation2){
                  query = query.Include("sub_relation2.sub_relation_of_sub_relation2");
              }
              return query;
          }
  </code></pre>
  
  <p>while i couldn't filter the relations in the include i did the following :</p>
  
  <pre><code>private ICollection&lt;SubRelation1&gt; _sub_relation1 {get; set;}
  public ICollection&lt;SubRelation1&gt; sub_relation1 {
  get 
  {
     //something like:
     return GlobalDefaultScope(_sub_relation1 ).ToList(); 
  }  
  set;}
  </code></pre>
  
  <p>while we filter the results in sub_relation1, when i do db.SaveChanges() the mentioned error is thrown.</p>