我们一直在考虑使用 Envers,但遇到了障碍。我们使用一个表/类结构,它为几个类使用每个类继承的表。当 NHibernate 和 Envers 正在加载时,当它尝试为子类创建表时会发生错误。
NHibernate.MappingException:无法为类 Demo.Vehicle_AUD 构建插入语句:添加鉴别器时发生故障 ---> System.ArgumentException:此 SQL 构建器中已添加列“VehicleTypeId”
参数名称:columnName
这是一个类似于我们正在使用的示例。
// Maps to Vehicle table
public Vehicle
{
public int VehicleId {get;set;}
pubic VehicleType VehicleTypeId {get;set;}
}
// Maps to Vehicle table with discriminator for VehicleTypeId == Car
public Car : Vehicle
{
public decimal MaxSpeed {get;set;}
}
// Maps to Vehicle table with discriminator for VehicleType == Airplane
public Airplane : Vehicle
{
public decimal MaxAirspeed {get;set;}
public decimal MaxAltitude {get;set;}
}
表定义:
VehicleId int identity primary key
VehicleTypeId int foreign key to VehicleTypeId on VehicleType table
MaxSpeed decimal null
MaxAirspeed decimal null
MaxAltitude decimal null
我们正在使用 FluentNHibernate:
var fluentConfig = FetchDbConfiguration(connectionString)
.Mappings(mapper =>
{
mapper.FluentMappings.AddFromAssemblyOf<Vehicle>()
})
.ExposeConfiguration(cfg =>
{
var enversConf = new FluentConfiguration();
//enversConf.Audit<Vehicle>();
enversConf.Audit<Car>();
enversConf.Audit<Airplane>();
nhConf.IntegrateWithEnvers(enversConf);
});
var nhConfiguration = fluentConfig.BuildConfiguration();
return nhConfiguration;
映射:
public partial class VehicleMap : ClassMap<Vehicle>
{
public VehicleMap()
{
Table("Vehicle");
LazyLoad();
Id(x => x.VehicleId)
.Column("VehicleId")
.CustomType("Int32")
.Access.Property()
.Not.Nullable()
.Precision(10)
.GeneratedBy.Identity();
DiscriminateSubClassesOnColumn("VehicleTypeId", 0)
.CustomType<int>()
.ReadOnly()
.SqlType("int")
.Not.Nullable();
}
}
public partial class CarMap : SubclassMap<Car>
{
public CarMap()
{
DiscriminatorValue(1); // 1 = Car
Map(x => x.MaxSpeed)
.Column("MaxSpeed")
.CustomType("Decimal")
.Access.Property()
.Generated.Never()
.Default(@"0")
.Precision(19)
.Scale(4);
}
}
使用 SubclassMap 将飞机与汽车类似地映射
该错误似乎正在发生,因为 Envers 正在尝试为两个子类创建 Vehicle 表。我尝试了包括/排除 Vehicle 类以进行审计的不同变体。
我的第一个问题是 Envers 是否支持每个类继承使用表?如果是这样,谁能指出我如何为每个班级的表格配置它的示例/文档?
谢谢。