Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to Entities - Reference EntityKey
    primarykey
    data
    text
    <p>Now that I have built a database. </p> <p>Visual Studio 2008 SP1’s ADO.NET Entity Framework has generated an Entity Model like this: <a href="http://img835.imageshack.us/img835/1810/carease15test1ef.png" rel="nofollow">http://img835.imageshack.us/img835/1810/carease15test1ef.png</a></p> <p>I have created a method as follow:</p> <pre><code> private void buttonAddPatient_Click(object sender, EventArgs e) { using (carease15test1Entities context = new carease15test1Entities()) { long bnumber = Convert.ToInt32(textBoxToBed.Text); long rnumber = Convert.ToInt32(textBoxOfRoom.Text); long bid = (from b in context.bed where b.bnumber == bnumber select b.bid).First(); long rid = (from r in context.room where r.rnumber == rnumber select r.rid).First(); // Create a new bed, and input its details. patient p = new patient(); p.pname = textBoxPatient.Text; p.Bed_bid = bid; p.bedReference.EntityKey = new EntityKey("carease15test1Entities.bed", "bid", bid); p.Bed_Room_rid = rid; p.bed.roomReference.EntityKey = new EntityKey("carease15test1Entities.room", "rid", rid); context.AddTopatient(p); try { context.SaveChanges(); } catch (OptimisticConcurrencyException ex) { // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. context.Refresh(System.Data.Objects.RefreshMode.ClientWins, p); // Save changes. context.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } </code></pre> <p>Everything works perfectly except for</p> <p>p.bed.roomReference.EntityKey = new EntityKey("carease15test1Entities.room", "rid", rid);</p> <p>It turns out a NullRefenceException saying “Object reference not set to an instance of an object.”</p> <p>Here is my project files.</p> <p><a href="http://www.mediafire.com/?w3rjjbxbxw6wacv" rel="nofollow">http://www.mediafire.com/?w3rjjbxbxw6wacv</a></p> <p>Could anyone give me some help?</p> <p>Thank you so much.</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.
 

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