nhibernate无法确定实体异常的类型

时间:2016-07-14 20:14:57

标签: c# nhibernate

我是流利的nhibernate新手。我正在开发一个项目,其数据库中包含以下3个表: “人”, “RealPerson”和 “LegalPerson”

这三个表的关系如图所示: enter image description here

我的代码中的所有实体(例如这三个实体)都是从基础实体类继承的,这里是这些实体的代码

public class Person : Entity
{
    public virtual RealPerson RealPerson { set; get; }
    public virtual LegalPerson LegalPerson { set; get; }

}
 public class RealPerson : Entity
{
    public virtual string FirstName { set; get; }
    public virtual string LastName { set; get; }
    public virtual string FatherName { set; get; }
    public virtual string NationalCode { set; get; }
    public virtual DateTime BirthDate { set; get; }

    public virtual string PhoneNumber { set; get; }
    public virtual string MobileNumber { set; get; }
    public virtual string EmailAddress { set; get; }
    public virtual string HomeAddress { set; get; }
    public virtual string WorkAddress { set; get; }
    public virtual RealPerson Proxy { set; get; }

}
public class LegalPerson : Entity
{
    public virtual string LegalPerson_Name { set; get; }
    public virtual string RegistrationNumber { set; get; }
    public virtual string Address { set; get; }
    public virtual string PhoneNumber1 { set; get; }
    public virtual string PhoneNumber2 { set; get; }
    public virtual string PhoneNumber3 { set; get; }
    public virtual RealPerson Proxy { set; get; }
}

并且基本实体类的代码在这里:

 public class Entity
{
    protected bool Equals(Entity other)
    {
        if (other == null)
            return false;
        if (Id == Guid.Empty || other.Id == Guid.Empty)
            return base.Equals(other);
        return Id.Equals(other.Id);


    }

    public virtual Guid Id { set; get; }
    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
    public override bool Equals(object obj)
    {
        if (obj is Entity)
            return Equals((Entity)obj);
        return base.Equals(obj);
    }
    protected ISession Session
    {
        get { return SessionAccountant.GetSession(); }
    }
    public virtual void Save()
    {
        Session.SaveOrUpdate(this);
    }

    public virtual void Delete()
    {
        Session.Delete(this);
    }
}

最后,类图如下:

  public class PersonMapping : ClassMap<Person>
{
    public PersonMapping()
    {
        Table("Person");
        Id(x => x.Id).GeneratedBy.GuidComb().Column("Person_Id");
        References(x => x.RealPerson).Nullable().LazyLoad().Column("RealPerson_Id");
        References(x => x.LegalPerson).Nullable().LazyLoad().Column("LegalPerson_Id");

    }
}
public class RealPersonMapping : ClassMap<RealPerson>
{
    public RealPersonMapping()
    {
        Table("RealPerson");
        Id(x => x.Id).GeneratedBy.GuidComb().Column("RealPerson_Id");
        Map(x => x.FirstName).Not.Nullable().Column("FirstName");
        Map(x => x.LastName).Not.Nullable().Column("LastName");
        Map(x => x.FatherName).Not.Nullable().Column("FatherName");
        Map(x => x.NationalCode).Not.Nullable().Column("NationalCode");
        Map(x => x.BirthDate).Nullable().Column("BirthDate");
        Map(x => x.ShenasnamehNumber).Nullable().Column("ShenasnamehNumber");
        Map(x => x.PhoneNumber).Nullable().Column("PhoneNumber");
        Map(x => x.MobileNumber).Nullable().Column("MobileNumber");
        Map(x => x.EmailAddress).Nullable().Column("EmailAddress");
        Map(x => x.HomeAddress).Nullable().Column("HomeAddress");
        Map(x => x.WorkAddress).Nullable().Column("WorkAddress");
        References(x => x.Proxy).Nullable().LazyLoad().Column("Proxy_Id");
    }
}
public class LegalPersonMapping : ClassMap<LegalPerson>
{
    public LegalPersonMapping()
    {
        Table("LegalPerson");
        Id(x => x.Id).GeneratedBy.GuidComb().Column("LegalPerson_Id");
        Map(x => x.LegalPerson_Name).Not.Nullable().Column("LegalPerson_Name");
        Map(x => x.RegistrationNumber).Not.Nullable().Column("RegistrationNumber");
        Map(x => x.Address).Not.Nullable().Column("Address");
        Map(x => x.PhoneNumber1).Nullable().Column("PhoneNumber1");
        Map(x => x.PhoneNumber2).Nullable().Column("PhoneNumber2");
        Map(x => x.PhoneNumber3).Nullable().Column("PhoneNumber3");
        References(x => x.Proxy).Nullable().LazyLoad().Column("Proxy_Id");

    }
}

我设置配置并创建会话。但是当我运行项目时,我在运行时获得此异常: NHibernate.dll

中发生了未处理的“NHibernate.MappingException”类型异常

其他信息:无法确定以下类型:EntitiesClasses.Person

这段代码出了什么问题?!

1 个答案:

答案 0 :(得分:0)

您需要继承SubclassMap。

请参阅此处http://notherdev.blogspot.com/2012/01/mapping-by-code-inheritance.html?m=1

我认为这会改变你的架构,因为你的基类不会拥有子类的id。基类的id是子类的id。