Search code examples
c#nhibernatetable-per-hierarchy

nHiberbate: Table per hierarchy - SchemaExport problem


I am using nhibernate to map the following classes:

public class DeviceConfig : EntityBase
{
    public virtual string Name
    {
        get { return m_Name; }
        set { SetValue(ref m_Name, value); }
    }
    public virtual string Description
    {
        get { return m_Description; }
        set { SetValue(ref m_Description, EmptyStringIfValueNull(value)); }
    }
}

 public class DeviceConfigEmail : DeviceConfig 
{
    public virtual string SmtpServer
    {
        get { return m_SmtpServer; }
        set { SetValue(ref m_SmtpServer, EmptyStringIfValueNull(value)); }
    }
}

public class DeviceConfigSMS : DeviceConfig
{
    public virtual string SmsServer
    {
        get { return m_SmsServer; }
        set { SetValue(ref m_SmsServer, EmptyStringIfValueNull(value)); }
    }

}

With the following Mapping file:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  default-access="field.pascalcase-m-underscore">
  <class name="MyNamespace.DeviceConfig, MyNamespace" table="DeviceConfig" lazy="false">
      <id name="ID" access="property" column="DeviceConfigID" unsaved-value="-1">
      <generator class="identity" />
    </id>

    <discriminator column="Discriminator"/>

    <version name="Version" column="Version"/>

    <property name="Name" not-null="true"/>
    <property name="Description" not-null="true"/>


    <subclass name="MyNamespace.DeviceConfigEmail, MyNamespace" discriminator-value="Email" lazy="false">
      <property name="SmtpServer" not-null="true"  />
    </subclass>

    <subclass name="MyNamespace.DeviceConfigSMS, MyNamespace" discriminator-value="SMS" lazy="false">
      <property name="SmsServer" not-null="true"  />
    </subclass>

  </class>
</hibernate-mapping>

I am doing table per hierarchy data modelling to a table as follows:

table: DeviceConfig
- DeviceConfigID (PK, int, not null)
- Discriminator (varchar(20), not null)
- Name (varchar(50), not null)
- Description (varchar(200), not null)
- SmtpServer (varchar(30), null)
- SmsServer (varchar(30), null)

This is all well and good, and works against my SQL database.

I now want to do a SchemaExport to do some in memory testing against Sqlite. When I generate this table with a schema export, both SmtpServer and SmsServer columns get generated as not null.

Is there something I am doing wrong? How can I get these columns to generate as nullable?

Thanks,

Paul


Solution

  • You've set the columns as not null in the mapping:

    <property name="SmtpServer" not-null="true" />
    ....
    <property name="SmsServer" not-null="true" /> 
    

    Remove the not-null attributes.