0

我尝试将一些通用属性提取到基类并使用 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 作为 Activate1_11_0_,items0_.Name 作为 Name12_0_,items0_.Description 作为 Descript2_12_0_,items0_.UnitPrice 作为 UnitPrice12_0_,items0_.Status 作为 Status12_0_,items0_.StoreId 作为 StoreId12_0_ FROM [Item] items0_ WHERE items0_.StoreId=?]"}

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

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

标识,版本

列是 Item 实体的一部分,并在 ItemMap 映射类中进行映射(使用 ItemMap 类继承自ClassMap<Item>相反,一切正常工作。

注意 我也尝试在 StoreMap 类上添加不成功。

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

有什么想法吗?

4

1 回答 1

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> { ... }

笔记:

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