简体   繁体   English

发生“ PatientInformation”对象的吸气剂的异常与目标类型不匹配。 C#.NET

[英]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. 我在C#网站上使用NHibernate,并且由于添加了PatientInformation域和映射而无法使其运行。 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. 我尝试过更新PatientInformationMap以包括ID字段,但它似乎无助于防止错误发生。 I've tried changing the many-to-many in the PatientInformationMap to HasManyToMany<Form> but the error persists. 我尝试将PatientInformationMap中的PatientInformationMapHasManyToMany<Form>但错误仍然存​​在。

Judging from what I have seen in other stackoverflow posts, there is something wrong in my mapping. 从我在其他stackoverflow帖子中看到的内容来看,我的映射有问题。 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. 我还没有看到没有使用该hbm文件的帖子,而且我什至不知道那是什么,所以我不确定这些帖子对我有什么帮助。

**** 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. ****我知道我有可能被别人拒绝,因为我的问题和其他人的问题相似,但我真的不明白与hbm文件有关的问题对我的情况有何帮助。 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添加数据的PatientInformation ,其中包括一些注释掉的代码,因为它无法正常工作,但它显示了我尝试过的内容。

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 编辑我更改了IList名称,以免与类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: 我在下面添加了.Inverse并收到此错误:

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

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. 您的HasManyToMany映射在映射中具有通用类型声明。 remove those types. 删除这些类型。 Fluent should be able to infer the type by the lambda expression you give it. Fluent应该能够通过您提供的lambda表达式来推断类型。

    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. 您说的是许多人都期望有一种表格,但是您正在将其映射到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. 还可以查看有关nhibernate映射的系列文章。 it is invaluable 这是无价的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM