title: Entity Framework: Can DB Contexts Have Foreign Keys Across Different Schemas?
body: <p>We have a single database with multiple schemas. Company architect wants to have Entity Framework DbContext for each schema. Additionally, foreign key relationships exist between different schemas.</p>
  
  <p>In scaffolding a schema today, received this error:</p>
  
  <p><strong>Error:</strong></p>
  
  <blockquote>
    <p>For foreign key FK_Customer_TransactionId on table CustomerTransaction, Unable to model the end of the foreign key on principal table inv.Product. This is usually because the principal table was not included in the selection set."</p>
  </blockquote>
  
  <p>I need to have one schema and 1 table from another schema. It cannot locate the Parent Table on the additional schema to create foreign key off. </p>
  
  <p>Looking for solutions:</p>
  
  <p>(1) Is it bad practice in general to have multiple db contexts for every schema, with foreign keys between different schemas? Do Microsoft and Entity Framework support this?</p>
  
  <p>(2) In Entity Framework Core Scaffold, Is there anyway to scaffold a schema And an additional parent table from another schema? </p>
  
  <p>(3) Or is there method to ignore foreign key property on outside schemas?</p>
  
  <pre><code>dotnet ef dbcontext scaffold 
     "Server=localhost;Database=DatabasetestTest;Trusted_Connection=True;" 
     Microsoft.EntityFrameworkCore.SqlServer -c DatabaseContext 
     -o Entities\Scaffold 
     -f 
     --project C:\Project 
     --schema CustomerService
  </code></pre>
  
  <p>Currently using EF Core 2.2</p>
  
  <p><a href="https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea">Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36576932/using-entity-framework-6-with-multiple-db-schemas-but-using-one-dbcontext">Using Entity Framework 6 with Multiple DB Schemas but using One DBContext</a></p>