title: Seed One-to-one Relationship ASP .NET MVC
body: <p>I am building a web application using ASP .NET MVC5 and EF6.</p>
  
  <p>I am trying to seed data to a one-to-one-relationship defined as such:</p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>public class Client 
  {
    public int ClientId { get; set; }  
    [Required]
    public string Name { get; set; }
  
    public Address Address { get; set; }
  }
  
  public class Address 
  {
    [ForeignKey("Client")]
    public int AddressId { get; set; }  
    [Required]
    public string StreetName { get; set; }
  
    public Client Client { get; set; }
  }
  </code></pre>
  
  <p>Where Address is the dependent end.</p>
  
  <p>Now, I want to seed these two tables using EF and in <strong>/Migrations/Configuration.cs</strong> I added the following code:</p>
  
  <pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;MR_TrackTrace.Models.ApplicationDbContext&gt;
      {
          public Configuration()
          {
              AutomaticMigrationsEnabled = false;
          }
  
          protected override void Seed(MR_TrackTrace.Models.ApplicationDbContext context)
          {
  
              var clients = new List&lt;Client&gt;
              {
                  new Client { Name = "John" },
                  new Client { Name = "Mary" },
                  new Client { Name = "Philip" }
              };
  
              clients.ForEach(s =&gt; context.Clients.AddOrUpdate(p =&gt; p.Name, s));
  
              context.SaveChanges();
              var addresses = new List&lt;Address&gt;
              {
                  new Address { StreetName = "something", Client = clients.FirstOrDefault(s =&gt; s.Name == "John") },
                  new Address { StreetName = "other", Client = clients.FirstOrDefault(s =&gt; s.Name == "Philip") },
                  new Address { StreetName = "another", Client = clients.FirstOrDefault(s =&gt; s.Name == "Mary") }
              };
  
              addresses.ForEach(s =&gt; context.Addresses.AddOrUpdate(p =&gt; p.Name, s));
              context.SaveChanges();
          }
   }
  </code></pre>
  
  <p>Now, after adding the migration and updating the database, I checked the tables and the Client is built and seeded according but the Address table has a column named ClientId as expected because it is the Foreign Key. </p>
  
  <p>But this column is not filled with the expected ID, instead it is filled with "0". By using:</p>
  
  <pre><code>Client = clients.FirstOrDefault(s =&gt; s.Name == "John")
  </code></pre>
  
  <p>I was expecting that the context would automatically set the ClientId for this table.</p>
  
  <p>Can anyone help me guiding me through this issue?
  Thanks in advance!</p>