Note that there are some explanatory texts on larger screens.

plurals
  1. POCode First to existing database - setting up a one to one relationship where TableA doesn't have an approprite foreign key reference to TableB
    primarykey
    data
    text
    <p>Ok, I have a strange situation while trying to map some POCO classes to a database that was not created by me.</p> <p>Here is the setup of the tables.</p> <pre><code>TableA ( TableA_ID int primary key, // other fields ) TableB ( TableB_ID int primary key, TableA_ID int unique not null, // other fields ) </code></pre> <p>So, how the tables are supposed to work together is that TableB has a reference to TableA - and it is a 1 to 1 relationship. TableA however does not have a foreign key to TableB's primary key.</p> <p>I can obviously set up the following relationships</p> <pre><code>public class TableA { public int TableA_ID { get; set; } public virtual ICollection&lt;TableB&gt; TableBs { get; set; } } public class TableB { public int TableB_ID { get; set; } public int TableA_ID { get; set; } public virtual TableA TableA { get; set; } } </code></pre> <p>With this setup I can say - this and it works ok:</p> <pre><code>TableA.TableBs.FirstOrDefault().SomeField; </code></pre> <p>But, what I really want is this</p> <pre><code>public class TableA { public int TableA_ID { get; set; } public virtual TableB TableB { get; set; } } public class TableB { public int TableB_ID { get; set; } public int TableA_ID { get; set; } public virtual TableA TableA { get; set; } } </code></pre> <p>With this setup I could then say:</p> <pre><code>TableA.TableB.SomeField; </code></pre> <p>Is there any way to map the TableA_ID from TableA to the TableA_ID from TableB to make the TableA class work how I want?</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.
 

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