Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would like to preface this answer with the disclaimer that I would not suggest using this solution on a regular basis. It is not the ideal way I would get this to work - but from a technical standpoint - for my particular situation - it should work out just fine.</p> <p>Ideally I would really like to have a TableB_ID field in the TableA table. That would then be an appropriate foreign key to the TableB table - and everything would work out great.</p> <p>That being said. Because the TableA_ID field on the TableB table is unique and not null - and - because you are able to specifically tell EF what to use as the primary key and foreign key - I was able to lie to EF and make it use for TableB the TableA_ID field as the primary key. Because of this - I was then able to tell TableA that its TableA_ID field was a foreign key to my TableB reference and all worked fine.</p> <p>Here is the fluent API bits I used to tell EF this lie.</p> <p>In my TableB EntityTypeConfiguration that I instantiate in the OnModelCreating override I say the following.</p> <pre><code>this.HasKey(t =&gt; t.TableA_ID); this.HasRequired(b =&gt; b.TableA).WithOptional(a =&gt; a.TableB); </code></pre> <p>This puts in the necessary bits to make everything point correctly for my situation. TableB ends up having a required link to TableA through the TableA_ID columns -and TableA has an optional link to TableB also through the TableA_ID columns.</p> <p>And, of course, since I have lied to EF and specifically told it that the TableA_ID field of TableB is the primary key - TableB_ID of the TableB table is not marked as such.</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