有两个具有许多公共字段的模型类,我决定创建一个基类并从它继承它们。
现有的模型类已经带有地图类。
现在在子类中继承的所有公共字段都是虚拟的,以保持 NHibernate 快乐,并且它们都映射正常,除了一个......
这是我的情况:
public class BaseClass : EntityBase<Guid>
{
//This is not complaining
public virtual string Text { get; set; }
//This is complaining
public virtual Guid TouchGuid { get; set; }
}
public class A : BaseClass
{
//inherited + specific stuff
}
public class B : BaseClass
{
//inherited + specific stuff
}
现在这些是映射类:
public class AMap : ClassMapping<A>
{
//Mapping the ID inherited from EntityBase class
Id(x => x.Id, mapper =>
{
mapper.Generator(Generators.GuidComb);
mapper.Column("Pk_MenuItemId");
});
//Mapping the common field inherited, doesn't complain !
Property(x => x.Mnemonic);
//This is the one which is complaining, keep in mind it was working
//before creating the base class and move the TouchGuid property in it.
Join("Touch", x =>
{
x.Key(k =>
{
k.Column("EntityId");
k.ForeignKey("PK_AClassId");
k.OnDelete(OnDeleteAction.Cascade);
k.Unique(true);
});
x.Property(p => p.TouchGuid);
});
}
public class BMap : ClassMapping<B>
{
//Same map as for class A
}
每当我运行程序,从这些类(表)中加载数据时,都会失败,说它在 A 表和 B 表上找不到 TouchGuid 列,这是错误:
是的,A 表和 B 表之间有共同的数据,但我无法更改数据库方案,现在它会增加太多复杂性。
我也需要为基类创建一个表吗?如果可能的话,我想避免创建一个新表。
有什么可能出错的提示吗?
谢谢 !