多层次的继承与抽象类流畅的nhibernate
本文关键字:nhibernate 继承 多层次 抽象类 | 更新日期: 2023-09-27 18:20:10
我使用的是VS2010、NHibernate 3.1.0.4000
和fluent 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
{
}
我的映射覆盖如下:
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上的映射覆盖,鉴别器被正确填写,医生和病人的top可以被持久化。
但后来我得到了错误:System.Data.SqlClient.SqlException : Invalid object name 'HUBSTOP'
所以我再次使用了表名,但现在我得到了以下错误:Cannot insert the value NULL into column 'StopType'
为什么表鉴别器一开始就不能在停止映射覆盖上工作?
尝试覆盖
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);
}