使用抽象类的多级继承可以使用nhibernate

时间:2013-06-28 07:17:32

标签: c# .net nhibernate inheritance fluent-nhibernate

我正在使用VS2010,NHibernate 3.1.0.4000fluent Nhibernate 1.2.0.712.

我的程序包含以下类层次结构:

public abstract class Stop
{  
}

public abstract class WorkStop : Stop
{
}

public class PatientStop : WorkStop
{
}

public class DoctorStop : WorkStop
{
}

public class HubStop : Stop
{
}

我的mappping覆盖如下:

 public class StopMappingOverride : IAutoMappingOverride<Stop>
{
    public void Override(AutoMapping<Stop> mapping)
    {
        mapping.DiscriminateSubClassesOnColumn("StopType");
        mapping.SubClass<HubStop>("HubStop");
        mapping.SubClass<WorkStop>("WorkStop").Abstract();
        mapping.References(x => x.Planning).Cascade.None();
    }
}

public class WorkStopMappingOverride : IAutoMappingOverride<WorkStop>
{
    public void Override(AutoMapping<WorkStop> mapping)
    {
        //mapping.Table("Stop");
        mapping.DiscriminateSubClassesOnColumn("StopType");
        mapping.SubClass<DoctorStop>("DoctorStop");
        mapping.SubClass<PatientStop>("PatientStop");
        mapping.HasMany(d => d.Tasks).KeyColumn("StopId")
            .AsSet()
            .Access.CamelCaseField(Prefix.Underscore);

        mapping.References(x => x.Doctor).Cascade.None();
    }
}

public class DoctorStopMappingOverride : IAutoMappingOverride<DoctorStop>
{
    public void Override(AutoMapping<DoctorStop> mapping)
    {
        mapping.References(x => x.Practice).Cascade.None();
        mapping.HasMany(d => d.Protocols).KeyColumn("StopId")
            .AsSet()
            .Access.CamelCaseField(Prefix.Underscore);
        mapping.HasMany(d => d.Materials).KeyColumn("StopId")
            .AsSet()
            .Access.CamelCaseField(Prefix.Underscore);
    }
}
public class PatientStopMappingOverride : IAutoMappingOverride<PatientStop>
{
    public void Override(AutoMapping<PatientStop> mapping)
    {
        mapping.References(x => x.Patient).Cascade.None();
    }
}

public class HubStopMappingOverride : IAutoMappingOverride<HubStop>
{
    public void Override(AutoMapping<HubStop> mapping)
    {
    }
}

以上用于以前的项目。除了工作停止的额外抽象级别,这是新的。  但是,当我在下面说明时,hubstop应该仍然可以工作。

当按原样使用映射覆盖时,我收到错误: System.Data.SqlClient.SqlException : Invalid object name 'WORKSTOP'

所以我想,如果我指定了表名,那是因为WorkStop上的映射覆盖正确地填充了鉴别器,并且可以保持医生和患者停留。

但后来我得到了错误: System.Data.SqlClient.SqlException : Invalid object name 'HUBSTOP'

所以我再次使用表名,但现在我收到以下错误: Cannot insert the value NULL into column 'StopType'

为什么表判别器首先在停止映射覆盖上工作?

1 个答案:

答案 0 :(得分:0)

尝试覆盖

public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool IsDiscriminated(System.Type type)
    {
        var types = new System.Type[] { typeof(Stop), typeof(WorkStop) };
        return base.IsDiscriminated(type) || types.Contains(type);
    }