title: EF6: Code First Complex Type
body: <p>I'm having trouble getting entity framework to flatten my domain entity classes with Value Objects (complex type) fields to one table. </p>
  
  <p>Everything works if I tell my model builder to ignore my value objects/complex type, but that results in all the attributes of the value object being missed in my tables. As soon as I remove the ignore statement i get "A value shared across entities is created in more than one location". If I look in the resulting CE SQL file I see an additional table named after my Domain class appended with a 1 and containing only the Value Object parameters.</p>
  
  <p>Some Code:</p>
  
  <p>My domain Classes:</p>
  
  <pre><code>public User {
  
      private User(){}
      public long Id {get; private set;} // dont ask, inherited legacy database
      public string UserId { get; private set; }
      public string Domain { get; private set; }
      public AuditIformation AuditDetails {get ; private set;}
  
      //..domain logic etc
  }
  
  public AuditInformation : IValueObject {
      public long CreatedByUserId { get; private set; }
      public DateTime CreatedDate { get; private set; }
  } 
  </code></pre>
  
  <p>My repository project (going code first) has got this:</p>
  
  <pre><code>public partial class myContext : DbContext {
  
      protected override void OnModelCreating(DbModelBuilder mb) {
  
          mb.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); 
  
          mb.ComplexType&lt;Domain.Model.AuditInformation&gt;();
          mb.ComplexType&lt;Domain.Model.AuditInformation&gt;().Property(a =&gt; a.CreatedDate).HasColumnName("Created_On");
          mb.ComplexType&lt;Domain.Model.AuditInformation&gt;().Property(a =&gt; a.CreatedByUserId).HasColumnName("Created_By");
  
          //This line lets everything work but doesn't include my 
          //AuditInformation attributes in my User Table.
          mb.Ignore&lt;Domain.Model.AuditInformation&gt;(); // &lt;== I think I need to remove this
  
          //..
  
          mb.Entity&lt;User&gt;().Map(a =&gt; {
              a.Property(x =&gt; x.Id).HasColumnName("Id");
              a.Property(x =&gt; x.UserId).HasColumnName("User_Id");
              a.Property(x =&gt; x.Domain).HasColumnName("User_Dmain");
              })
          .HasKey(x =&gt; x.Id)
          .ToTable("Tbl_User");   //&lt;==Again, dont ask
  
    }
  }
  </code></pre>
  
  <p>What I want to get is a table looking like:</p>
  
  <pre><code>[TBL_USER] 
  ID AS BIGINT,
  USER_ID as VARCHAR(MAX),
  USER_DMAIN AS VARCHAR(MAX),
  CREATED_ON as DATE,
  CREATED_BY as BIGINT
  </code></pre>
  
  <p>But what im getting is only:</p>
  
  <pre><code>[TBL_USER] 
  ID AS BIGINT,
  USER_ID as VARCHAR(MAX),
  USER_DMAIN AS VARCHAR(MAX),
  </code></pre>
  
  <p>and if I remove the ignore line i get this bonus freak table</p>
  
  <pre><code>[USER1]  &lt;&lt;==Note, named after the domain class, not the destination table.. 
  ID AS BIGINT,
  CREATED_ON as DATE,
  CREATED_BY as BIGINT
  </code></pre>
  
  <p>and a whole bunch of error when I try to use my repository:</p>
  
  <pre><code>----&gt; System.Data.Entity.Infrastructure.DbUpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
  ----&gt; System.Data.Entity.Core.UpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
  ----&gt; System.ArgumentException : An item with the same key has already been added.
  TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Ive done a lot of searching but I just cant find any concrete examples of persisting my value object attributes into the tables created for my domain objects. Can someone show me where I'm going wrong?</p>