当基类在流畅的nhibernate中是抽象的时,如何实现每个具体类的表?
我有以下
public abstract class BaseClass
{
public virtual int Id {get; set};
public virtual string Name {get; set;}
}
public class FirstSubClass : BaseClass
{
//properties and behaviour here
}
public class SecondSubClass : BaseClass
{
//properties of SecondSubclass Here
}
public class ProcessStep
{
public virtual IList<BaseClass> ContentElements {get; set;}
}
映射场景,我使用了以下代码片段:-
this._sessionFactory =
Fluently.Configure().Database(SQLiteConfiguration.Standard
.ConnectionString(@"Data Source=SqliteTestSqlDataAccess.s3db; Version=3; New=True; Pooling=True; Max Pool Size=1;"))
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assemblyWithDomainClasses).Conventions.Add(DefaultCascade.All())))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
默认情况下,Fluent 将忽略抽象基类 BaseClass。 但正如在类 ProcessStep 中一样,有一个属性 ContentElements 返回 IList ,我遇到了一个异常:- NHibernate.MappingException :关联引用未映射的类:BaseClass
如果我使用 IncludeBase(typeof(BaseClass)) 包含基类,那么它工作正常,但它会为 BaseClass 和派生类创建一个表,并且记录与 FK-PK 关系链接( 每个子类的表)。 我想要实现的是每个具体类的表。也就是说,每个派生类都有自己的表,其中包含派生类的所有属性+基类中的属性。 知道如何实现它吗?
i have the following scenario
public abstract class BaseClass
{
public virtual int Id {get; set};
public virtual string Name {get; set;}
}
public class FirstSubClass : BaseClass
{
//properties and behaviour here
}
public class SecondSubClass : BaseClass
{
//properties of SecondSubclass Here
}
public class ProcessStep
{
public virtual IList<BaseClass> ContentElements {get; set;}
}
for mapping i have used following code snippet :-
this._sessionFactory =
Fluently.Configure().Database(SQLiteConfiguration.Standard
.ConnectionString(@"Data Source=SqliteTestSqlDataAccess.s3db; Version=3; New=True; Pooling=True; Max Pool Size=1;"))
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assemblyWithDomainClasses).Conventions.Add(DefaultCascade.All())))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
By default fluent will ignore the abstract base class that is BaseClass.
But as in the class ProcessStep there is property ContentElements which returns IList , i am getting an exception:-
NHibernate.MappingException : Association references unmapped class: BaseClass
If i include the base class using the IncludeBase(typeof(BaseClass)) then it works fine but it creates a table for BaseClass and Derived classes and the records are linked with FK-PK relationship(table per subclass).
What i want to achieve is table per concrete class. that is each derive class will have it's own table where there will all properties of derived class + properties in the base class.
Any idea how to achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于我还没有看到你的地图,所以让我提供我的。你可以通过这样做来实现这一点
Since I haven't seen your mapping, let me provide mine. You could achieve this by doing like this
使用带有 nhibernate 自动映射的抽象基类来实现“每个具体类一个表”继承策略让我很头疼。但我想,我终于找到了解决方案并想与您分享。我还认为,它没有添加到自动映射文档中,因为它可能被视为“弱”数据库设计。
首先,这是我找到的有关此主题的一些资源:
流畅的 nhibernate 中继承策略的示例实现(!automapping)。
具有自动映射功能的 Fluent nhibernate 继承策略的文档。
Bug-Fix Union-SubClass 实现(每个具体类的表)
这些资源基本上描述了您需要如何执行此操作:
It caused me headache to implement the "Table per Concrete Class" inheritance strategy with an abstract base class with nhibernate automapping. But I think, I've finally found a solution and want to share it with you. I also think, it's not added to the automapping docs, because it's maybe considered as a "weak" database design.
First here are some resources I found about this topic:
Example implementation of inheritance strategies in fluent nhibernate (!automapping).
Documentation of inheritance strategies in fluent nhibernate with automapping.
Bug-Fix Union-SubClass implementation (Table per Concrete Class)
These resources basically describe how you need to do it: