title: Entity Framework - Inserting entity with multiple models and databases
body: <p>I have my domain split into multiple Entity Framework models. I have some shared entities that span multiple models (named Lookup), however, these are replaced with "using" references using the methods described in <a href="http://blogs.msdn.com/b/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx" rel="noreferrer">Working With Large Models In Entity Framework</a>. However, what makes my case slightly more unique is that I'm also separating these models into multiple databases (one per model).</p>
  
  <p>I'm having a problem inserting one of my shared entities into my common DB. It's failing with the error:</p>
  
  <blockquote>
    <p>The member with identity
    'Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup'
    does not exist in the metadata
    collection.</p>
  </blockquote>
  
  <p>That foreign key that it's referring to does <strong>not</strong> exist on the "common DB". But I'm also <strong>not</strong> working with the entity on the other side of the relationship (named ResidentialAddress); nor do I even have the context that would contain the other entity initialized (named MembersDb). However, both models are compiled into the same assembly.</p>
  
  <p>There are no navigation properties going <em>from</em> Lookup to ResidentialAddress. Though there is a navigation property in the other direction (which I won't be persisting - only using in memory).</p>
  
  <p>My <code>MetadataWorkspace</code> for the <code>EntityConnection</code> of the CommonDb context was explicitly initialized with only the SSDL/CSDL/MSL for the data required for that database. I have confirmed there is no references to the foreign key mentioned in that set of schema data.</p>
  
  <pre><code>var metaAssembly = typeof(CommonDb).Assembly;
  var schemaResources = new string[]
  { 
      String.Format("res://{0}/Common.ssdl", metaAssembly.FullName), 
      String.Format("res://{0}/Common.csdl", metaAssembly.FullName), 
      String.Format("res://{0}/Common.mdl", metaAssembly.FullName), 
  }
  MetadataWorkspace metadata = new MetadataWorkspace(schemaResources, new []{ metaAssembly });
  EntityConnection connection = new EntityConnection(metadata, myDatabaseConnection);
  </code></pre>
  
  <p><strong>POSSIBLE CLUE:</strong> It does work when I go into the generated classes and remove all of the <code>EdmRelationshipAttribute</code> attributes along with their paired <code>EdmRelationshipNavigationPropertyAttribute</code> from the related models (MembersDb).</p>
  
  <p><strong>Key questions:</strong></p>
  
  <ol>
  <li><p>So why is it that Entity Framework is trying to do something with the relationship that is for an entity that is neither in scope and nor will it be affected by the insertion of the record!?</p></li>
  <li><p>I am happy to have the generated code remove the attributes mentioned above, but I still want the navigation properties to remain. How would I go about altering the CSDL to achieve that?</p></li>
  </ol>
  
  <p>NOTE: Persistence of the "child" models is not a priority, nor is the integrity of their now cross-DB foreign keys. These databases are persisted using SQL CE but they were originally generated from a single master SQL Server database.</p>