当我使用流畅的Nhibernate进行继承时,尝试运行查询实体

时间:2012-11-20 16:19:26

标签: nhibernate c#-4.0 fluent-nhibernate

我尝试将一些常见属性提取到基类并使用Fluent Nhibernate进行映射。此外,我还尝试添加第二级继承。

//Base entity class
public class EntityBase : IEntityBase
{
    public EntityBase()
    {
        CreatedDate = DateTime.Now;
    }
    public virtual DateTime? CreatedDate { get; set; }
    public virtual int Id { get; set; }
    public virtual int Version { get; set; }
}

//Base Entity Mapping
public class EntityBaseMap: ClassMap<EntityBase>
{
     public EntityBaseMap()
     {
         UseUnionSubclassForInheritanceMapping();
         Id(x => x.Id);
         Version(x => x.Id);
         Map(x => x.CreatedDate);
     }
}



//first sub class of EntityBase
public class Actuate : EntityBase, IActuate
{
    public virtual DateTime? ActivatedOn { get; set; }
}

//Actuate Mapping class
public class ActuateMap : SubclassMap<Actuate>
{
     public ActuateMap()
     {
         Map(x => x.ActivatedOn);
     }
}

//Sub class entity 
public class Item : Actuate
{
  public virtual string Name { get; set; }
  public virtual string Description { get; set; }
  public virtual decimal UnitPrice { get; set; }
  public virtual ItemStatus Status { get; set; }
  public virtual Store Store { get; set; }
}

//Item Mapping class
public class ItemMap : SubclassMap<Item>
{
public ItemMap()
{
        Abstract();
  Map(x => x.Name);
  Map(x => x.Description);
  Map(x => x.UnitPrice);
  Map(x => x.Status);
  References(x => x.Store);
 }

}

我发现的实体存在问题(可能存在其他关系问题)

//Store entity Does not inherit from EntityBase or Actuate
public class Store
{
  public virtual int Id { get; set; }
  public virtual int Version { get; set; }
  public virtual string Name { get; set; }
  public virtual IEnumerable<Item> Items { get; set; }
 }      

//Store mapping class
public class StoreMap : ClassMap<Store>
{
 public StoreMap()
 {
   Id(x => x.Id).GeneratedBy.Assigned();
   Version(x => x.Version);
   Map(x => x.Name);
   HasMany(x => x.Items);
 }
}

问题

如果我尝试运行以下查询:

//store = is the Store entity I have retrieved from the database and I am trying  
//trying to return the items that are associated with the store and are active

store.Items != null && store.Items.Any(item => item.Status == ItemStatus.Active);

我收到以下错误:

错误

  

Nhibernate.Exceptions.GenericADOException:无法初始化集合:[SomeDomain.Store.Items#0] [SQL:SELECT items0_.StoreId as StoreId1_,items0_.Id as Id1_,items0_.Id as Id10_0_,items0_.CreatedDate as CreatedD2_10_0_,items0_.ActivatedOn as Activate1_11_0_,items0_.Name as Name12_0_,items0_.Description as Descript2_12_0_,items0_.UnitPrice as UnitPrice12_0_,items0_.Status as Status12_0_,items0_.StoreId as StoreId12_0_ FROM [Item] items0_ WHERE items0_.StoreId =?]“ }

内部异常 “无效的对象名称'项目'。”

现在,如果我取出基类并且Item不继承,那么

  

Id,版本

列是Item实体的一部分,并在ItemMap映射类中映射(而ItemMap类继承自ClassMap<Item>,所有内容都可以正常运行。

注意 我还试图在StoreMap类上添加不成功。

HasMany(x => x.Items).KeyColumn("Id");

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果entityBase仅用于继承公共属性,那么您根本不需要映射它

public class EntityBaseMap<TEntity> : ClassMap<TEntity> where TEntity : EntityBase
{
     public EntityBaseMap()
     {
         Id(x => x.Id);
         Version(x => x.Version);
         Map(x => x.CreatedDate);
     }
}

public class ActuateMap : EntityBaseMap<Actuate> { ... }

备注:

  • 版本映射应映射版本属性而不是Id
  • 版本应该是代码只读,所以没有人意外地改变它
  • .KeyColumn("Id")错误,因为该列来自Items表,然后它是自动生成的id和外键。
  • 这是不可能的也不是有用的
  • 通常只有抽象的类应该在映射
  • 中包含Abstract()