Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework Code First optional one-to-one relationship with no reverse navigation property
    primarykey
    data
    text
    <p>I am trying to model what I think is quite a basic scenario, but I can't get my head around the correct way to define it in EF Code First 5.</p> <p>I have three classes:</p> <p>Employee Postal Address Contact Telephone Number</p> <p>The rules are thus:</p> <ul> <li>A Postal Address can be standalone</li> <li>A Contact Telephone Number can be standalone</li> <li>Optionally, an Employee can have a "Home" Postal Address</li> <li>An Employee can have zero or more Contact Telephone Numbers</li> </ul> <p>If I model this in SQL, I end up with four tables;</p> <ul> <li>PostalAddress</li> <li>ContactTelephoneNumber</li> <li>Employee</li> <li>EmployeeContactTelephoneNumber (bridge table)</li> </ul> <p>By looking through the answers here on SO, I can produce something similar to my SQL model in Code First with the exception that I have to have an Employee navigation property on PostalAddress and an Employees navigation property on ContactTelephoneNumber. This goes against my business rules, because neither PostalAddress nor ContactTelephoneNumber will always be refered to be an Employee. For example, I could later add a Premises class which would also have a PostalAddress.</p> <p>By way of code examples, what I have now resembles the following:</p> <pre><code>public class Employee { public int EmployeeID {get;set;} public virtual PostalAddress? HomeAddress {get;set;} } public class PostalAddress { public int PostalAddressID {get;set;} public string Address {get;set;} // It's not actually a string - this is for brevity! } public class ContactTelephoneNumber { public int ContactTelephoneNumberID {get;set;} public string TelephoneNumber {get;set;} // It's not actually a string - this is for brevity! } </code></pre> <p>I'm trying to define my relationships using the Fluent API rather than Annotations, to ensure my presentation layer remains unaware of Entity Framework when consuming these classes. My mapping currently resembles the following - is this "correct"?</p> <pre><code>public class EmployeeMap : EntityTypeConfiguration&lt;Employee&gt; { public EmployeeMap() { this.HasOptional(e =&gt; e.HomeAddress).WithOptionalDependent(p =&gt; p.Value).Map(m =&gt; m.MapKey("PostalAddressID")); } } </code></pre> <p>This doesn't compile; I get:</p> <blockquote> <p>The type 'PostalAddress?' must be a reference type in order to use it as parameter 'TTargetEntity' in the generic type or method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration.HasOptional(System.Linq.Expressions.Expression>)'</p> </blockquote> <p>Additionally, I don't like that I'm passing "PostalAddressID" as a string constant into the call to MapKey().</p> <p>Please could someone highlight the error of my ways? I've been searching for the past 3 hours to no avail!</p> <p>Edit: I should mention that once I understand this part I'll try and address the Employee>ContactTelephoneNumbers situation separately.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload