“PatientInformation”对象的getter与目标类型不匹配。 C#.NET

时间:2016-09-12 16:22:01

标签: c# asp.net nhibernate

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

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

1 个答案:

答案 0 :(得分:1)

今天,心灵调试并不适合我。但我想我只是看到了它。您的HasManyToMany映射在映射中具有泛型类型声明。删除这些类型。 Fluent应该能够通过你给它的lambda表达式推断出类型。

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

直接冲突。你说的是很多人都期望一个Form,但是你将它映射到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();
    }
}

另请查看nhibernate映射上的这一系列帖子。这是非常宝贵的

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