title: Entity Framework 6 Code first One-to-Many relation in 3rd level child tables
body: <p>I have <em><code>ApplicationUser</code></em> table created by ASP.NET identity framework and want to use another table Customer with <code>one-to-one relation</code>. The <code>PK</code> of <code>ApplicationUser</code> table is <code>Id</code> of type string. And the <code>Customer</code> table has a <code>FK</code> of type string. I am not allowed to change the <code>Id</code> type for <code>ApplicationUser</code> table. But customer table must have an identity(auto number) column with int type. This is fine.</p>
  
  <p>And then this <code>Customer</code> table has a <code>one-to-many</code> relation with <code>Contact</code> table. 
  <a href="https://i.stack.imgur.com/tYHKW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tYHKW.png" alt="enter image description here"></a></p>
  
  <p>Both <code>Customer</code> and <code>Contact</code> tables have unique identity column <code>Id</code>s. The <code>Contact</code> table has a <code>CustomerId</code> Column. If there is no <code>AspNetUser</code> table, code first generates <code>one-to-many</code> relation using these <code>id</code> and <code>CustomerId</code> columns. Since the <code>AspNetuser</code> table has <code>one-to-one</code> relation with <code>Customer</code>, this doesn’t use <code>CustomerId</code>. But the code first generate its own string key <code>(Customer_UserId)</code> to build this <code>one-to-many</code> relation.</p>
  
  <p>How do I use <code>Customer</code> table column to relate <code>Contact</code> tables <code>CustomerId</code> column to map the relation using EF6 code first approach?</p>
  
  <p>Here is the complete code:</p>
  
  <p><strong>ApplicationUser</strong></p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      //Navigation   properties
      public Customer Customer { get; set; }
  
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
      {
          var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
          return userIdentity;
      }
  }
  </code></pre>
  
  <p><strong>Customer</strong></p>
  
  <pre><code>public class Customer
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [Index(IsUnique = true)]
      public int Id { get; set; }
  
      [Key, ForeignKey("ApplicationUser")]
      public string UserId { get; set; }
  
      public string BusinessName { get; set; }
  
      //Navigation properties
      [ForeignKey("UserId")]
      public ApplicationUser ApplicationUser { get; set; }
      public virtual ICollection&lt;Contact&gt; Contacts { get; set; }
  }
  </code></pre>
  
  <p><strong>Contact</strong></p>
  
  <pre><code>public class Contact
  {
      public int Id { get; set; }
      public int CustomerId { get; set; }
  
      public string LastName { get; set; }
      public string FirstName { get; set; }
      public string OfficeLocation { get; set; }
      //Navigation properties
      [Required]
      public Customer Customer { get; set; }
  
  }
  </code></pre>
  
  <p><strong>ApplicationDbContext</strong></p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext(): base("DefaultConnection", throwIfV1Schema: false){}
  
      static ApplicationDbContext()
      {
          // Set the database intializer which is run once during application start
          // This seeds the database with admin user credentials and admin role
          Database.SetInitializer&lt;ApplicationDbContext&gt;(new ApplicationDbInitializer());
      }
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
          modelBuilder.Entity&lt;Customer&gt;()
              .HasRequired(u =&gt; u.ApplicationUser).WithRequiredDependent(c =&gt; c.Customer);
      }
  
      public System.Data.Entity.DbSet&lt;WebServer.Models.Customer&gt; Customers { get; set; }
  
      public System.Data.Entity.DbSet&lt;WebServer.Models.Contact&gt; Contacts { get; set; }
  }
  </code></pre>