title: Entity framework map entity to multiple tables in cascade
body: <p>I'm facing a problem using EF.
  I have the following situation:</p>
  
  <p><img src="https://i.stack.imgur.com/777u8.png" alt="DB schema"></p>
  
  <p>From this database schema i'd like to generate the following entity by merge tables data:</p>
  
  <pre><code>// Purchases
      public class Purchase
      {
          //Fields related to Purchases
          public int IdPurchase { get; set; }
          public string CodPurchase { get; set; }
          public int IdCustomer { get; set; }
          public decimal Total { get; set; }
  
          //Fields related to Customers table
          public string CodCustomer { get; protected set; }
          public string CompanyTitle { get; protected set; }
          public string CodType { get; protected set; }
  
          //Fields related to CustomersType table
          public string DescrType { get; protected set; }
      }
  </code></pre>
  
  <p>As you can see, in my context i don't want 3 separated entities for each table. I want a single one with the fields related to all tables. All fields of Customers and CustomersType tables must be readonly (so i've set the relative setters protected) and the others must be editables so that EF can track changes. In particular, i'd like to have the ability to change the "IdCustomer" field and let EF to automatically update "CodCustomer", "CompanyTitle", "DescrType"....and so on by doing cross table select.</p>
  
  <p>To do that, i wrote this configuration class:</p>
  
  <pre><code> internal class PurchaseConfiguration : EntityTypeConfiguration&lt;Purchase&gt;
      {
          public PurchaseConfiguration(string schema = "dbo")
          {
              ToTable(schema + ".Purchases");
              HasKey(x =&gt; x.IdPurchase);
  
              Property(x =&gt; x.IdPurchase).HasColumnName("IdPurchase").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
              Property(x =&gt; x.IdCustomer).HasColumnName("IdCustomer").IsRequired();
              Property(x =&gt; x.Total).HasColumnName("Total").IsRequired().HasPrecision(19, 4);
  
              Map(mc =&gt;
               {
                   mc.Properties(n =&gt; new
                   {
                       n.CodCustomer,
                       n.CompanyTitle,
                       n.CodType
                   });
                   mc.ToTable("Customers");
               });
  
              Map(mc =&gt;
              {
                  mc.Properties(n =&gt; new
                  {
                      n.DescrType,
                  });
                  mc.ToTable("CustomersType");
              });
  
          }
      }
  </code></pre>
  
  <p>I've tested it but it doesn't work as expected. I always get this message: </p>
  
  <blockquote>
    <p>Properties for type 'Purchase' can only be mapped once. The non-key
    property 'CodCustomer' is mapped more than once.  Ensure the
    Properties method specifies each non-key property only once.</p>
  </blockquote>
  
  <p>Maybe there's something wrong or i forget something (for example the join fields of Map&lt;> that i don't know where to specify them).
  How can i accomplish in the correct way this task?
  I don't want to have "Customers" and "CustomersType" DBSets in my context.
  Is there a way to avoid it?</p>
  
  <p>I even thought to add into the "IdCustomer" setter a custom query to update manually "Customers" and "CustomersType" related fields, but i don't want to do that for 2 reasons:</p>
  
  <ol>
  <li>I don't have any DbConnection avaiable into the "Purchases" class, so i can't create a DbCommand to read data from DB.</li>
  <li>I want entity class to be persistent-ignorant</li>
  <li>EF seems to be a powerfull tool that can do these sort of things and i don't want to reinvent the wheel by writing custom procedures.</li>
  </ol>
  
  <p>I've uploaded the example C# source and the tables CREATE scripts (MS SQLServer) <a href="https://dl.dropboxusercontent.com/u/60231308/testCascadeEntityReference.rar" rel="nofollow noreferrer">here</a>.
  All entities are autogenerated by the "EF reverse POCO generator" T4 template (the T4 template is disabled, to activate it set CustomTool = TextTemplatingFileGenerator).
  Do not forget to update the ConnectionString in the app.config.</p>
  
  <p>Thanks in advance.</p>