title: Entity Framework 4 - Mapping non-public properties with CTP5 (code first) in a persistence unaware context
body: <p>I know the question already has a solution (eg. <a href="https://stackoverflow.com/questions/3577891/entity-framework-ctp4-code-first-mapping-protected-properties">this question</a>) but I really can't afford to attach the mapping logic in the same assembly where the domain (POCO classes) is.</p>
  
  <p>Is there any other way?</p>
  
  <p>I found this <a href="http://daniel.wertheim.se/2009/11/10/entity-framework-4-relationships-between-non-public-members/" rel="nofollow noreferrer">nice blog post</a> but I couldn't get it working.
  Here is the model:</p>
  
  <pre><code>public class Institute
  {
      /**
          Code omitted
      **/
  
      protected virtual ICollection&lt;InstituteText&gt; InnerInstituteTexts { get; set; }
  
      private InstituteTextSet _TextSets;
  
      public InstituteTextSet Texts 
      {
          get 
          {
              if (_TextSets == null)
                  _TextSets = new InstituteTextSet(InnerInstituteTexts);
  
              return _TextSets;
          }
      }
  }
  </code></pre>
  
  <p>Mapping code:</p>
  
  <pre><code>var instituteTextExpression = ObjectAccessor&lt;Institute&gt;.CreateExpression&lt;ICollection&lt;InstituteText&gt;&gt;("InnerInstituteTexts");
  
  institute.HasMany(instituteTextExpression)
      .WithRequired()
      .HasForeignKey(t =&gt; t.InstituteId);
  </code></pre>
  
  <p>where CreateExpression is defined as:</p>
  
  <pre><code>public static Expression&lt;Func&lt;T, TResult&gt;&gt; CreateExpression&lt;TResult&gt;(string propertyOrFieldName)
  {
      ParameterExpression param = Expression.Parameter(typeof(T), "propertyOrFieldContainer");
      Expression body = Expression.PropertyOrField(param, propertyOrFieldName);
      LambdaExpression lambda = Expression.Lambda(typeof(Func&lt;T, TResult&gt;), body, param);
  
      return (Expression&lt;Func&lt;T, TResult&gt;&gt;) lambda;
  }
  </code></pre>
  
  <p>the error I get is:</p>
  
  <blockquote>
    <p>Initialization method
    Studentum.Core.Tests.InstituteTests.Initialize
    threw exception.
    System.TypeInitializationException:
    System.TypeInitializationException:
    The type initializer for
    'Studentum.Core.FluentCoreRepositoryFactory'
    threw an exception. --->
    System.InvalidOperationException: The
    configured property
    'InnerInstituteTexts' is not a
    declared property on the entity
    'Institute'. Verify that it has not
    been explicitly excluded from the
    model and that it is a valid primitive
    property..</p>
  </blockquote>