C# Linq to Entities - Reference.EntityKey
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: C# Linq to Entities - Reference.EntityKey

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    C# Linq to Entities - Reference.EntityKey

    C# Linq to Entities - Reference.EntityKey


    Now that I have built a database and here is the corresponding SQL code:

    http://www.mediafire.com/?f4a0d74j7qe7a1f



    Visual Studio 2008 SP1’s ADO.NET Entity Framework has generated an Entity Model like this:


    http://postimage.org/image/lulmehljv/

    I have created some methods as follows:

    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);
    
                    }
    
                }


    Everything works perfectly except for

    p.bed.roomReference.EntityKey = new EntityKey("carease15test1Entities.room", "rid", rid);

    in the method private void buttonAddPatient_Click(object sender, EventArgs e).

    It turns out a NullRefenceException saying “Object reference not set to an instance of an object.”

    Here is my project files.

    http://www.mediafire.com/?w3rjjbxbxw6wacv

    Could anyone give me some help?

    Thank you so much.
    Attached Images Attached Images  
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center