Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand it, Entity Framework enforces one-to-one relationships by making the 2 entities share the same primary key. That means that there is no need for a separate foreign key column, because the primary key property also acts as the foreign key.</p> <p>So, remove the surplus foreign keys and change the type of the Item's key to string so that it can refer to the User's Id:</p> <pre><code>class User : IdentityUser // so it has public string Id! { public virtual Item Item { get; set; } } class Item { public string Id { get; set;} public virtual User User { get; set; } } </code></pre> <p>Now, when both ends of the relationship are required or both sides are optional the Entity Framework cannot identify the dependent and principal. The dependent is the one that gets the foreign key referencing the principal's key.</p> <p>So try this:</p> <pre><code>modelBuilder.Entity&lt;User&gt;() .HasOptional(f =&gt; f.Item). //Item is dependent and //gets the foreign key .WithOptionalPrincipal(s =&gt; s.User); </code></pre> <p><strong>Proviso</strong></p> <p>I haven't tried this or done this and the bit I don't get is this. If both ends are optional, then how can the <code>Item</code> have a foreign key to the <code>User</code> that is also a primary key? A primary key is required but an optional foreign key is not. So they cannot be one and the same.</p> <p>So, if EF has now added An optional foreign key, I would add a unique index to it to ensure it is 1 to 1. As it is nullable, you will need to do it using sql in a migration</p> <p><code>Sql("CREATE INDEX ON Item(User_Id) UNIQUE WHERE User_Id IS NOT NULL")</code></p> <p>References:</p> <p><a href="http://msdn.microsoft.com/en-us/data/jj713564" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/data/jj713564</a></p> <p><a href="http://msdn.microsoft.com/en-us/data/jj591620.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/data/jj591620.aspx</a></p> <p>It says on that page: </p> <blockquote> <p>When both ends of the relationship are optional, use WithOptionalPrincipal or WithOptionalDependent after the HasOptional method.</p> </blockquote> <p><a href="https://stackoverflow.com/questions/19580054/do-i-define-a-relationship-between-two-entities-on-the-dependent-or-the-principa/19580798#19580798">Do I define a relationship between two entities on the dependent or the principal?</a></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. 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