当基类是流利的nhibernate抽象时,如何实现每个具体类的表? [英] how to achieve table per concrete class when base class is abstract in fluent nhibernate?

查看:164
本文介绍了当基类是流利的nhibernate抽象时,如何实现每个具体类的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

$ p $ public abstract class BaseClass
{
public virtual int Id {得到;组};
公共虚拟字符串名称{get; set;}
}

public class FirstSubClass:BaseClass
{
//在这里的属性和行为
}

public class SecondSubClass:BaseClass
{
// SecondSubclass的属性这里
}

公共类ProcessStep
{
公共虚拟IList< BaseClass> ContentElements {get; set;}
}

用于映射我已经使用下面的代码片段: -

  this._sessionFactory = 
流利的.Configure()。数据库(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:关联引用未映射如果我使用IncludeBase(typeof(BaseClass))包含基类,那么它工作正常,但它为BaseClass和派生类和记录创建一个表与FK-PK关系(每个子类的表)关联。
我想实现的是每个具体类的表。也就是说每个派生类都有它自己的表,其中将有基类中派生类+属性的所有属性。
任何想法如何实现它?

解决方案

由于我没有看到你的映射,让我提供我的。您可以通过这样做来实现这一点。

  public class BaseClassMap:ClassMap< BaseClass> 
public BaseClassMap()
{
/ *
*身份生成器不能是本地的,因为子类对象应该是唯一的
*所以使用HiLo或Guid或其他生成器将在子表上生成唯一的ID
* /
Id(x => x.Id).GeneratedBy.Guid();
Map(x => x.Name);
UseUnionSubclassForInheritanceMapping(); //这是重要的 - 对派生类使用联合子类映射


$ b public class FirstSubClassMap:SubclassMap< FirstSubClass>
{
public FirstSubClassMap()
{
表(FirstSubClassTable);
// FirstSubClass的映射属性
}
}

public class SecondSubClassMap:SubclassMap< SecondSubClass>
{
Public SecondSubClassMap()
{
表(SecondSubClassTable);
// SecondSubClass的地图属性
}
}


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?

解决方案

Since I haven't seen your mapping, let me provide mine. You could achieve this by doing like this

public class BaseClassMap:ClassMap<BaseClass>
{
    public BaseClassMap()
    {
        /*
         * Identity generator can't be native because subclass objects should be unique
         * So use HiLo or Guid or other generators which will generate unique id on the child tables
         */
        Id(x => x.Id).GeneratedBy.Guid(); 
        Map(x => x.Name);
        UseUnionSubclassForInheritanceMapping(); // This is important - uses union-subclass mappings for the derived classes
    }
}

public class FirstSubClassMap : SubclassMap<FirstSubClass>
{
    public FirstSubClassMap()
    {
        Table("FirstSubClassTable");
        // Map properties for FirstSubClass
    }
}

public class SecondSubClassMap : SubclassMap<SecondSubClass>
{
    public SecondSubClassMap()
    {
        Table("SecondSubClassTable");
        // Map properties for SecondSubClass
    }
}

这篇关于当基类是流利的nhibernate抽象时,如何实现每个具体类的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆