简体   繁体   English

流利的抽象类的多层次继承

[英]Multiple levels of inheritance with abstract classes fluent nhibernate

I'm using VS2010, NHibernate 3.1.0.4000 and fluent Nhibernate 1.2.0.712. 我正在使用VS2010, NHibernate 3.1.0.4000fluent Nhibernate 1.2.0.712.

My program contains the following class hierarchy: 我的程序包含以下类层次结构:

public abstract class Stop
{  
}

public abstract class WorkStop : Stop
{
}

public class PatientStop : WorkStop
{
}

public class DoctorStop : WorkStop
{
}

public class HubStop : Stop
{
}

My mappping override is as follows: 我的映射覆盖如下:

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

The above used to work in previous projects. 以上用于以前的项目。 except for the extra level of abstraction which is workstop, thats new. 除了工作停止这一额外的抽象级别之外,这是新的。 But then hubstop should still work as i'll illustrate below. 但是集线器仍然可以正常工作,如下所述。

When is use the mapping override as is, i get the error: System.Data.SqlClient.SqlException : Invalid object name 'WORKSTOP' 什么时候按原样使用映射覆盖,我得到错误: System.Data.SqlClient.SqlException : Invalid object name 'WORKSTOP'

So i thought, what if i specify the table name, and yes because of the mapping override on WorkStop the discriminator is filled in correctly and doctor and patientstops can be persisted. 所以我想,如果我指定表名怎么办,是的,因为在WorkStop上的映射覆盖,鉴别符已正确填写,并且Doctor和Patientstop可以保留。

But then i get the error: System.Data.SqlClient.SqlException : Invalid object name 'HUBSTOP' 但是然后我得到了错误: System.Data.SqlClient.SqlException : Invalid object name 'HUBSTOP'

So again i used the table name, but now i get the following error: Cannot insert the value NULL into column 'StopType' 因此,我再次使用了表名,但是现在出现以下错误: Cannot insert the value NULL into column 'StopType'

Why doesn't the table discriminator work in the first place, on the stop mapping override? 为什么表识别符首先在停止映射覆盖上不起作用?

Try to override 尝试覆盖

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM