Jamie
Jamie

Reputation: 1679

Exception occurred getter of 'PatientInformation' Object does not match target type. C# .NET

I'm using NHibernate in my C# website and am having trouble trying to get it to run since the addition of the PatientInformation domain and mapping. If someone can review the code below and point to the area that is wrong, I would really appreciate the help.

I've tried updating the PatientInformationMap to include the ID field, but it doesn't seem to help prevent the error from occuring. I've tried changing the many-to-many in the PatientInformationMap to HasManyToMany<Form> but the error persists.

Judging from what I have seen in other stackoverflow posts, there is something wrong in my mapping. I haven't seen any posts that aren't using that hbm file, and I don't even know what that is so I'm not sure how those posts would help me much.

**** I'm aware I'm at risk of being voted down because my question is similar to others', but I really don't see how something pertaining to an hbm file is helpful to my situation. Thanks in advance for answering instead of immediately trying to get this question removed.

Exception occurred getter of Form.PatientInformation

Object does not match target type.
at CommonSessionManager.UnbindCurrentSession() in \CommonSessionManager.cs:line 54
at CommonSessionManager.Application_EndRequest(Object sender, EventArgs e) in \CommonSessionManager.cs:line 25
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

************* Mapping

public PatientInformationMap()
{
        Schema("FormsLibrary");
        Table("PatientInformation");
        Map(x => x.FullName);
        Map(x => x.DateOfBirth);
        Map(x => x.ContactAccount);

        HasManyToMany<PatientInformation>(x => x.Forms)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("PatientInformationID")
            .ChildKeyColumn("FormID")
            .LazyLoad()
            .Cascade.SaveUpdate();
    }
}


 public FormMap()
 {
        Schema("FormsLibrary");
        Table("Form");
        Map(x => x.Title);
        Map(x => x.Description);
        Map(x => x.FileName);
        Map(x => x.MembersOnly);
        Map(x => x.Status);
        Map(x => x.Active);
        Map(x => x.DisplayFileName);
        Map(x => x.LastModified);
        Map(x => x.CreatedDate);

        References(x => x.User, "UserID").LazyLoad();
        References(x => x.Site, "SiteID").LazyLoad();
        References(x => x.Category, "CategoryID").Cascade.SaveUpdate().LazyLoad();

        HasManyToMany<Form>(x => x.PatientInformation)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("FormID")
            .ChildKeyColumn("PatientInformationID")
            .LazyLoad()
            .Cascade.SaveUpdate();
    }
}

************* Domain

 public class PatientInformation : EntityBase<int>
 {
    public PatientInformation()
    {
        this.Forms = new List<Form>();
    }

    public virtual IList<Form> Forms { get; set; }
    public virtual string FullName { get; set; }
    public virtual string DateOfBirth { get; set; }
    public virtual string ContactAccount { get; set; }
 }

public class Form : OrderedEntityBase<int>
{
    public Form()
    {
        this.Active = true;
        this.LastModified = DateTime.Now;
        this.CreatedDate = DateTime.Now;
        this.PatientInformation = new List<PatientInformation>();
    }
    public Form(Site site)
    {
        this.Site = site;
        this.Active = true;
        this.LastModified = DateTime.Now;
        this.CreatedDate = DateTime.Now;
        this.PatientInformation = new List<PatientInformation>();
    }
    public Form(Site site, aspnet_User user)
    {
        this.User = user;
        this.Site = site;
        this.Active = true;
        this.LastModified = DateTime.Now;
        this.CreatedDate = DateTime.Now;
        this.PatientInformation = new List<PatientInformation>();
    }

    public virtual void AddCategory(Category category)
    {
        this.Category = category;
        category.Forms.Add(this);
    }
    public virtual Category Category { get; set; }
    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
    public virtual string FileName { get; set; }
    public virtual string DisplayFileName { get; set; }
    public virtual Site Site { get; protected set; }
    public virtual bool MembersOnly { get; set; }
    public virtual string Status { get; set; }
    public virtual bool Active { get; set; }
    public virtual aspnet_User User { get; set; }
    public virtual DateTime LastModified { get; set; }
    public virtual DateTime CreatedDate { get; protected set; }
    public virtual IList<PatientInformation> PatientInformation { get; set; }

    // do not map
    public virtual void AddPatientInformation(PatientInformation patientInformation)
    {
        if (this.HasPatientInformation(patientInformation))
        {
            this.RemovePatientInformation(patientInformation);
        }
        patientInformation.Forms.Add(this);
        this.PatientInformation.Add(patientInformation);
    }
    public virtual void RemovePatientInformation(PatientInformation patientInformation)
    {
        patientInformation.Forms.Remove(this);
        this.PatientInformation.Remove(patientInformation);
    }
    public virtual bool HasPatientInformation(PatientInformation patientInformation)
    {
        return this.PatientInformation.Contains(patientInformation);
    }
    public virtual void ClearPatientInformation()
    {
        var deletePatientInformation = new List<PatientInformation>();
        foreach (var patientInformation in this.PatientInformation)
        {
            deletePatientInformation.Add(patientInformation);
        }
        foreach (var patientInformation in deletePatientInformation)
        {
            this.RemovePatientInformation(patientInformation);
        }
    }
}

This is where I am adding data to PatientInformation, which includes some commented out code because it wasn't working, but it shows what I have tried.

PatientInformation patientInfo = new PatientInformation();

StatusPlaceHolder.Visible = true;
form.Status = StatusRadioButtonList.SelectedValue != null ? StatusRadioButtonList.SelectedValue : null;

PatientPlaceHolder.Visible = true;
patientInfo.FullName = PatientNameTextBox.Text != null ? PatientNameTextBox.Text : null;
patientInfo.DateOfBirth = DateOfBirthTextBox.Text != null ? DateOfBirthTextBox.Text : null;
patientInfo.ContactAccount = ContactAccountTextBox.Text != null ? ContactAccountTextBox.Text : null;
// need to get form ID to associate this patientinfoID to the formID in PatientInformationToForms table
//form.PatientInformation.Add(patientInfo);
//patientInfo.Form.Add(form);
form.AddPatientInformation(patientInfo);

EDITS I changed the IList name so as not to confuse with the class PatientInformation

 public virtual IList<PatientInformation> PatientInformationList { get; set; }
HasManyToMany<Form>(x => x.PatientInformationList)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("FormID")
            .ChildKeyColumn("PatientInformationID")
            .LazyLoad()
            .Cascade.SaveUpdate();

EDIT

I added .Inverse below and got this error:

The relationship PatientInformation.Forms to PatientInformation.Forms has Inverse specified on both sides. Remove Inverse from one side of the relationship.

 HasManyToMany<PatientInformation>(x => x.Forms)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("PatientInformationID")
            .ChildKeyColumn("FormID")
            .LazyLoad()
            .Inverse()
            .Cascade.SaveUpdate();

Upvotes: 0

Views: 130

Answers (1)

Fran
Fran

Reputation: 6520

Psychic debugging isn't working for me today. But I think I just saw it. Your HasManyToMany mappings has generic type declaration in the mappings. remove those types. Fluent should be able to infer the type by the lambda expression you give it.

    HasManyToMany<Form>(x => x.PatientInformation)

is in direct conflict. You are saying the many to many is expectings a Form, but you are mapping it to a PatientInformation. remove that type declaration from both sides of your mapping.

public PatientInformationMap()
{
        Schema("FormsLibrary");
        Table("PatientInformation");
        Map(x => x.FullName);
        Map(x => x.DateOfBirth);
        Map(x => x.ContactAccount);

        HasManyToMany(x => x.Forms)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("PatientInformationID")
            .ChildKeyColumn("FormID")
            .LazyLoad()
            .Cascade.SaveUpdate();
    }
}

 public FormMap()
 {
        Schema("FormsLibrary");
        Table("Form");
        Map(x => x.Title);
        Map(x => x.Description);
        Map(x => x.FileName);
        Map(x => x.MembersOnly);
        Map(x => x.Status);
        Map(x => x.Active);
        Map(x => x.DisplayFileName);
        Map(x => x.LastModified);
        Map(x => x.CreatedDate);

        References(x => x.User, "UserID").LazyLoad();
        References(x => x.Site, "SiteID").LazyLoad();
        References(x => x.Category, "CategoryID").Cascade.SaveUpdate().LazyLoad();

        HasManyToMany(x => x.PatientInformation)
            .Schema("FormsLibrary")
            .Table("PatientInformationToForms")
            .ParentKeyColumn("FormID")
            .ChildKeyColumn("PatientInformationID")
            .LazyLoad()
            .Cascade.SaveUpdate();
    }
}

Also check out this series of posts on nhibernate mappings. it is invaluable

http://notherdev.blogspot.com/2012/01/mapping-by-code-onetomany-and-other.html

Upvotes: 1

Related Questions