title: EF 4.3 Code First one-to-one relationship when using an existing database
body: <p>I have two tables in my database which have a one-to-one relationship. I want to access them with EF using the code first approach. I have written the POCO classes like this:</p>
  
  <pre><code>public class User
  {
      public int Id { get; set; }
      public string Login { get; set; }
      public string Password { get; set; }
      public string Email { get; set; }
  
      public virtual Profile Profile { get; set; }
  }
  
  public class Profile
  {
      public int UserId { get; set; }
      public string FullName { get; set; }
      public DateTime? Birthday { get; set; }
      public bool? Male { get; set; }
  }
  </code></pre>
  
  <p>And also I described the relationships between that classes in the OnModelCreating as explained <a href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" rel="nofollow">here</a>. So that's how looks:</p>
  
  <pre><code>modelBuilder.Entity&lt;Profile&gt;().HasKey(x =&gt; x.UserId);
  modelBuilder.Entity&lt;User&gt;().
      HasOptional(u =&gt; u.Profile).
      WithRequired();
  </code></pre>
  
  <p>But when I run the application I'm getting this exception when trying to access the User tables:</p>
  
  <blockquote>
    <p>Invalid column name "User_Id"</p>
  </blockquote>
  
  <p>Although when I let EF create a new database on its own with these two tables, it creates a database that has <strong>the same structure</strong> as mine and it works.</p>
  
  <p>I was using EF 4.3 (I tried 4.2 too) and MS SQL 2008. What am I doing wrong? I can't believe that it's a bug of Entity Framework.</p>