Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Error mapping table per subclass in Fluent NHibernate

Error mapping table per subclass in Fluent NHibernate

Scheduled Pinned Locked Moved ASP.NET
databasehelpsysadminxml
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AumSingh
    wrote on last edited by
    #1

    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
    Storage

    Database table "dbo.SamsungPhone" details: Following are the columns in the table.

    SamsungPhoneID (Identity Primary Kay)
    SmartPhoneBaseID (Foreign Key to dbo.SmartPhoneBase)
    AndroidVersion
    QualcommProcessorTech
    AMOLEDDisplayTech

    Entity 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.

    A 1 Reply Last reply
    0
    • A AumSingh

      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
      Storage

      Database table "dbo.SamsungPhone" details: Following are the columns in the table.

      SamsungPhoneID (Identity Primary Kay)
      SmartPhoneBaseID (Foreign Key to dbo.SmartPhoneBase)
      AndroidVersion
      QualcommProcessorTech
      AMOLEDDisplayTech

      Entity 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.

      A Offline
      A Offline
      AumSingh
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups