getter 'PatientInformation'对象与目标类型不匹配.c# . net

本文关键字:不匹配 net 类型 对象 PatientInformation getter 目标 | 更新日期: 2023-09-27 18:06:44

我在我的c#网站中使用NHibernate,并且由于添加了PatientInformation域和映射,我很难让它运行。如果有人可以检查下面的代码并指出错误的地方,我将非常感谢帮助。

我已经尝试更新PatientInformationMap以包括ID字段,但它似乎无助于防止错误的发生。我已经尝试将PatientInformationMap中的多对多更改为HasManyToMany<Form>,但错误仍然存在。

从我在其他stackoverflow帖子中看到的情况来看,我的映射有问题。我没有看到任何不使用hbm文件的帖子,我甚至不知道那是什么,所以我不确定这些帖子如何帮助我很多。

****我知道我有被否决的风险,因为我的问题与他人相似,但我真的看不出与hbm文件有关的东西对我的情况有什么帮助。提前感谢你的回答,而不是立即试图删除这个问题。

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)

************* 映射

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

************* 域

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

这是我在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);

编辑我更改了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();

编辑

我在下面添加了.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();

getter 'PatientInformation'对象与目标类型不匹配.c# . net

通灵调试今天对我不起作用。但我想我刚刚看到了。你的hasmanymany映射在映射中有泛型类型声明。删除这些类型。Fluent应该能够通过给出的lambda表达式推断类型。

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

是直接冲突的。您说多对多期望一个表单,但是您将它映射到一个PatientInformation。从映射的两边删除该类型声明。

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

也可以查看这个关于非hibernate映射的系列文章。它是无价的

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