title: How to define foreign key relationship in EF Core 2.1
body: <p>I am using EF Core 2.1 for my DAL. This is how my model looks like</p>
  
  <p>One user can have only one role:</p>
  
  <pre><code>// Role entity - kind of master
  public class Role
  {
      public int RoleId { get; set; }
  
      public string RoleName { get; set; }
  }
  
  // User entity - every user will have one role
  public class User
  {
      public int UserId { get; set; }
  
      public string Email { get; set; }
      public string Password { get; set; }
  
      public int RoleId { get; set; }
      public bool IsActive { get; set; }
      public DateTime CreatedOn { get; set; }
  
      public virtual Role Role { get; set; }
  }
  </code></pre>
  
  <p>This is how I was thinking/tried to define the relationship between these two entities:</p>
  
  <pre><code>public void Configure(EntityTypeBuilder&lt;User&gt; builder)
  {
      builder.HasKey(x =&gt; x.UserId);
      builder.Property(x =&gt; x.RoleId).IsRequired(true);
      builder.Property(x =&gt; x.CreatedOn).IsRequired(true).HasDefaultValue(DateTime.Now);
  
      // FK - throwing compile time error 
      builder.HasOne(x =&gt; x.Role).WithOne(y =&gt; y.RoleId);
  }
  </code></pre>
  
  <p>How do I define this one user one role relation using EF Fluent API?</p>
  
  <p>Thanks.</p>
  
  <p>Attachment:-
  <a href="https://i.stack.imgur.com/Nmefi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nmefi.png" alt="enter image description here"></a></p>