0
votes

i am new to fluent nhibernate. i'm developing a project contains following 3 tables in its database: "Person", "RealPerson" and "LegalPerson"

these three tables have relation as shown in picture: enter image description here

all entities in my code such as these three entities are inherited from a base entity class here is the code of these entities

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

and the code of base entity class is here:

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

Finally the classmaps are as following:

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

    }
}

i set the configuration and create a session. but when i run the project i get this exception at run time: An unhandled exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

Additional information: Could not determine type for: EntitiesClasses.Person

what is wrong with this code?!

1

1 Answers

0
votes

You need to inherit from SubclassMap.

See here http://notherdev.blogspot.com/2012/01/mapping-by-code-inheritance.html?m=1

I think this is going to change your schema in that your base class isn't going to have the id of the subclass. The id of the base class is the id of the subclass.