none
One to None/One Relationship RRS feed

  • Question

  • I have two tables, one being Patient and the other being a PatientMap. A Patient entity may have either one or no associated entry in the PatientMap. I want to be able to make a select to pull a patient and get the PatientMap entry as well, if one exists, or just have a null for the field, if no associated entry.

    I can setup my entities to work fine with a one to one relationship, but the moment I pull a Patient with no PatientMap entry in the database, I get: "Object reference not set to an instance of an object." Here is what I have so far:

      [Table(Name="Patient")]
      class Patient : IPatient
      {
        private int _ID;
        private EntityRef<PatientMap> _PatMap;
        //..
    
        [Column(Name = "p_id", CanBeNull = false, IsPrimaryKey = true, Storage = "_ID")]
        public int ID
        {
          get { return _ID; }
          set
          {
            if (_ID != value)
            { _ID = value; }
          }
        }
    
        [Association(Name = "Patient_PatientMap", Storage = "_PatMap", ThisKey = "ID", OtherKey = "ID", IsForeignKey = false)]
        public PatientMap PatMap
        {
          get { return _PatMap.Entity; }
          set { _PatMap.Entity = value; } 
        }
      }
    
      [Table(Name = "pat_id_map")]
      class PatientMap : IPatientMap
      {
        private int _ID;
        private string _ExternalID;
        private string _Location;
        private EntityRef<Patient> _Patient;
    
        [Column(Name = "p_id", CanBeNull = true, Storage = "_ID")]
        public int ID
        {
          get { return _ID; }
          set
          {
            if (_ID != value)
            {
              _ID = value;
            }
          }
        }
    
        [Column(Name = "ExternalID", Storage = "_ExternalID", IsPrimaryKey = true)]
        public string ExternalID
        {
          get { return _ExternalID; }
          set
          {
            if (_ExternalID != value)
            {
              _ExternalID = value;
            }
          }
        }
    
        [Association(Name = "Patient_PatientMap", Storage = "_Patient", ThisKey = "ID", OtherKey = "ID", IsForeignKey = true)]
        public Patient PatientRef
        {
          get { return _Patient.Entity; }
          set { _Patient.Entity = value; }
        }
      }
    
    I've tried looking around, but all the examples I can find are for one to one or one to many relationships. Any insight would be greatly appreciated. Thanks.
    Wednesday, March 23, 2011 6:08 PM

Answers

  • well, I guess you posted to much info except what is important, which is how you are querying the linq. any way, here is how I do it

     

    Try
    
     dim lq_parent = (From p In db.parent
    
         Where p.id = txt_parentid.text
    
         Select p).First
    
    Catch ex As Exception
    
     MsgBox("parent not found")
    
     Exit Sub
    
    End Try
    
    If lq_parent.child is Nothing
    
     MsgBox ("no child found")
    
    Else
    
     dim lq_child = lq_parent.child
    
    End If
    
    
    
    

     

    ofcource, you should make the relation in the DBML between the 2 table.

    hth.


    EDIT BY Jackie Sun:

    @Samir, thank you very much for your contribution!

    @Nyxation, I'm afraid you need to translate this code to C#. :)

    I hope this can help you!

    Saturday, March 26, 2011 4:31 PM