title: Setting up optional:required relationship Entity Framework - fluent API
body: <p>I have a USER - PROFILE relation where the user can (optional) have a profile associated.
  In the database the PROFILES table has a FK to the USERS table (the FK is nullable).</p>
  
  <p>I want to setup this with EF fluent API. I managed to do it and everything works ok but the problem is i cannot have the FK from the profile as a property in the classes.</p>
  
  <p>So in the DB i have:</p>
  
  <pre><code>[USERS]
  ID uniqueidentifier PK not null
  
  [PROFILES]
  ID uniqueidentifier PK not null
  USERID uniqueidentifer FK null
  </code></pre>
  
  <p>And in the code i have:</p>
  
  <pre><code>public class User { public virtual Profile { get; set; } }
  public class Profile { public virtual User { get; set; } }
  </code></pre>
  
  <p>Note that the column names in the DB does not go along with EF naming convention.</p>
  
  <p>I can configure EF as follows:</p>
  
  <pre><code>HasOptional(u =&gt; u.Profile).WithRequired(p =&gt; p.User).Map(m =&gt; m.MapKey("USERID")).WillCascadeOnDelete();
  </code></pre>
  
  <p>or the other way around:</p>
  
  <pre><code>HasRequired(p =&gt; p.User).WithOptional(u =&gt; u.Profile).Map(m =&gt; m.MapKey("USERID")).WillCascadeOnDelete();
  </code></pre>
  
  <p>I really need in this case to map the FK using the MapKey (because of the naming convention EF uses) and this seems to be the problem that won't let me add the FK in the model also.</p>
  
  <p>So i cannot have:</p>
  
  <pre><code>public class Profile { public virtual User { get; set; } public Guid? UsreId { get; set; } }
  </code></pre>
  
  <p>Anyone has an idea how can i accomplish this?</p>
  
  <p><strong>EDIT</strong>
  The problem source is the fact that if I don't use .Map(m => m.MapKey("USERID")) method the convention breaks the relationship (it expects that the FK is User_Id) which basically is like using it without any params. If i use .Map(m => m.MapKey("USERID")) method then it says that i already have the UserId as a property in the model (from inline help:  Configures the relationship to use foreign key property(s) that <strong>are not exposed in the object model</strong>. The column(s) and table can be customized by specifying a configuration action. If an empty configuration action is specified then column name(s) will be generated <strong>by convention</strong>).</p>
  
  <p>Bummer...</p>
  
  <p><strong>EDIT 2</strong> Managed to fix it somehow (basically going for another approach).
  Changed the tables to:</p>
  
  <pre><code>[USERS]
  ID uniqueidentifier PK not null
  
  [PROFILES]
  -- ID uniqueidentifier PK not null (removed the PK)
  USERID uniqueidentifer PK not null, FK not null -- (the PK is also the FK)
  </code></pre>
  
  <p>I ended up here after running the SQL profiler and looking at the queries ran there.
  The optionl PROFILE for each user was queried based on USERID. The were clause was something like:</p>
  
  <pre><code>SELECT *
  FROM PROFILES
  WHERE ID = @userId -- ID was the PK
  </code></pre>
  
  <p>So i figured out that the PK from PROFILES table must also be the FK in this case, and the mappings can be kept simple:</p>
  
  <pre><code>HasOptional(u =&gt; u.Profile).WithRequired(p =&gt; p.User);
  </code></pre>
  
  <p>or on the other end</p>
  
  <pre><code>HasRequired(p =&gt; p.User).WithOptional(u =&gt; u.Profile);
  </code></pre>
  
  <p>And this one was helpfull: <a href="http://msdn.microsoft.com/en-us/library/hh295843%28v=vs.103%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/hh295843%28v=vs.103%29.aspx</a></p>
  
  <p>Cheers!</p>