Error mapping table per subclass in Fluent NHibernate
-
I will give details of the infrastructure I have built and will talk about the problem in the end. Database table "dbo.SmartPhoneBase" details: Following are the columns in the table.
SmartPhoneBaseID (Identity Primary Key)
Company
PhoneName
RAM
StorageDatabase table "dbo.SamsungPhone" details: Following are the columns in the table.
SamsungPhoneID (Identity Primary Kay)
SmartPhoneBaseID (Foreign Key to dbo.SmartPhoneBase)
AndroidVersion
QualcommProcessorTech
AMOLEDDisplayTechEntity classes for the above tables:
public class SmartPhoneBase
{
public virtual int SmartPhoneBaseID { get; private set; }public virtual string Company { get; set; } public virtual string PhoneName { get; set; } public virtual string RAM { get; set; } public virtual string Storage { get; set; } }
public class SamsungPhone:SmartPhoneBase
{
public virtual string AndroidVersion { get; set; }public virtual string QualcommProcessorTech { get; set; } public virtual string AMOLEDDisplayTech { get; set; } }
Mapping class for the above entity classes:
public class SmartPhoneBaseMap:ClassMap
{
public SmartPhoneBaseMap()
{
Schema("dbo");
Table("SmartPhoneBase");Id(x => x.SmartPhoneBaseID) .Column("SmartPhoneBaseID") .GeneratedBy.Identity(); Map(x => x.Company).Length(100).Not.Nullable(); Map(x => x.PhoneName).Length(100).Not.Nullable(); Map(x => x.RAM).Length(100).Not.Nullable(); Map(x => x.Storage).Length(100).Not.Nullable(); } }
public class SamsungPhoneMap:SubclassMap
{
public SamsungPhoneMap()
{
Schema("dbo");
Table("SamsungPhone");Map(x => x.AndroidVersion).Length(100).Not.Nullable(); Map(x => x.QualcommProcessorTech).Length(100).Not.Nullable(); Map(x => x.AMOLEDDisplayTech).Length(100).Not.Nullable(); } }
Please note following points. 1. For the subclass "SamsungPhone" the primary key is not allowed to be mapped with the method Id() hence I did not keep the property for same reason in the subclass even though its corresponding table "dbo.SamsungPhone" has a primary key.
-
I will give details of the infrastructure I have built and will talk about the problem in the end. Database table "dbo.SmartPhoneBase" details: Following are the columns in the table.
SmartPhoneBaseID (Identity Primary Key)
Company
PhoneName
RAM
StorageDatabase table "dbo.SamsungPhone" details: Following are the columns in the table.
SamsungPhoneID (Identity Primary Kay)
SmartPhoneBaseID (Foreign Key to dbo.SmartPhoneBase)
AndroidVersion
QualcommProcessorTech
AMOLEDDisplayTechEntity classes for the above tables:
public class SmartPhoneBase
{
public virtual int SmartPhoneBaseID { get; private set; }public virtual string Company { get; set; } public virtual string PhoneName { get; set; } public virtual string RAM { get; set; } public virtual string Storage { get; set; } }
public class SamsungPhone:SmartPhoneBase
{
public virtual string AndroidVersion { get; set; }public virtual string QualcommProcessorTech { get; set; } public virtual string AMOLEDDisplayTech { get; set; } }
Mapping class for the above entity classes:
public class SmartPhoneBaseMap:ClassMap
{
public SmartPhoneBaseMap()
{
Schema("dbo");
Table("SmartPhoneBase");Id(x => x.SmartPhoneBaseID) .Column("SmartPhoneBaseID") .GeneratedBy.Identity(); Map(x => x.Company).Length(100).Not.Nullable(); Map(x => x.PhoneName).Length(100).Not.Nullable(); Map(x => x.RAM).Length(100).Not.Nullable(); Map(x => x.Storage).Length(100).Not.Nullable(); } }
public class SamsungPhoneMap:SubclassMap
{
public SamsungPhoneMap()
{
Schema("dbo");
Table("SamsungPhone");Map(x => x.AndroidVersion).Length(100).Not.Nullable(); Map(x => x.QualcommProcessorTech).Length(100).Not.Nullable(); Map(x => x.AMOLEDDisplayTech).Length(100).Not.Nullable(); } }
Please note following points. 1. For the subclass "SamsungPhone" the primary key is not allowed to be mapped with the method Id() hence I did not keep the property for same reason in the subclass even though its corresponding table "dbo.SamsungPhone" has a primary key.
Well guys I just got the answer to the above problem. I just missed this mapping in the class SamsungPhoneMap
KeyColumn("SmartPhoneBaseID");
I did not see this being mentioned anywhere, not even the Fluent Nhibernate talks about it. But all's well that ends well, I got the answer at last.