Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not sure if you want the following:</p> <pre class="lang-cs prettyprint-override"><code>modelBuilder.Entity&lt;Customers&gt;() .HasMany(c =&gt; c.Phones) .WithOptional() .Map(m =&gt; m.MapKey("CustomerPhoneId")); modelBuilder.Entity&lt;Companies&gt;() .HasMany(c =&gt; c.Phones) .WithOptional() .Map(m =&gt; m.MapKey("CompanyPhoneId")); </code></pre> <p>Using <code>MapKey</code> is optional. It just gives the foreign key column the name you want. If you omit this EF will create a standard name (something with underscore: <code>..._Id</code>).</p> <p>In fact the whole mapping is optional because the mapping conventions will just create the same relationships without any Fluent API mapping at all.</p> <p>The <code>Phones</code> table will have two nullable foreign keys <code>CustomerPhoneId</code> and <code>CompanyPhoneId</code> refering to <code>Customers</code> table and <code>Companies</code> table respectively.</p> <p><strong>Edit</strong></p> <p>An alternative which would only require one foreign key in the <code>Phone</code> table for multiple different entities would be an inheritance mapping:</p> <pre class="lang-cs prettyprint-override"><code>public abstract class OrganizationsWithPhone { public OrganizationsWithPhone() { Phones = new List&lt;Phones&gt;(); } public Guid Id { get; set; } public ICollection&lt;Phones&gt; Phones { get; set; } } public class Customers : OrganizationsWithPhone { public string FirstName { get; set; } public string LastName { get; set; } } public class Companies : OrganizationsWithPhone { public string Name { get; set; } } public class Phones { public Guid Id { get; set; } public string Number { get; set; } public string Extension { get; set; } } </code></pre> <p>Mapping:</p> <pre class="lang-cs prettyprint-override"><code>modelBuilder.Entity&lt;OrganizationsWithPhone&gt;() .HasMany(o =&gt; o.Phones) .WithOptional() // or .WithRequired() .Map(m =&gt; m.MapKey("OrganizationsWithPhoneId")); modelBuilder.Entity&lt;OrganizationsWithPhone&gt;() .ToTable("OrganizationsWithPhone"); modelBuilder.Entity&lt;Customers&gt;() .ToTable("Customers"); modelBuilder.Entity&lt;Companies&gt;() .ToTable("Companies"); </code></pre> <p>Now you have only one foreign key relationship between <code>Phones</code> and the base table <code>OrganizationsWithPhone</code>, but due to the inheritance mapping there are additional one-to-one relationships between the base table and the tables for the derived entities <code>Customers</code> and <code>Companies</code>. Basically the number of necessary relationships stays the same (or is even one more in this model).</p> <p>In this model a customer and a company cannot share the same phone numbers because a row in the <code>Phones</code> table refers to an <code>OrganizationsWithPhone</code> which can either be a customer or a company but not both at the same time.</p> <p>The base table <code>OrganizationsWithPhone</code> has only a single column <code>Id</code>. If you have more common properties in all derived entities you can move them into the base entitiy.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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