我使用FluentNHibernate和AutoMapping。 没有使用自定义约定或更改。 NHibernate和FluentNHibernate程序集都是最新版本。数据库是Sqlite3
我尝试使用以下实体(每个层次结构的表):
public abstract class Unit
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Employee : Unit
{
public Employee()
{
this.Groups = new List<Group>();
}
public virtual IList<Group> Groups { get; private set; }
}
public class Group : Unit
{
public Group ()
{
this.Employees = new List<Employee>();
}
public virtual int EmployeesCount { get; set; }
public virtual IList<Employee> Employees { get; private set; }
}
public class GroupAutoMappingOverride : IAutoMappingOverride<Group>
{
public void Override (AutoMapping<Group> mapping)
{
mapping.Map(g => g.EmployeesCount).Formula("count(*)");
}
}
我自动生成架构。一切都很好:
create table "Unit" (
Id integer,
TypeId TEXT not null, //discriminator column
Name TEXT,
primary key (Id)
)
create table EmployeesToGroups (
Employee_id INTEGER not null,
Group_id INTEGER not null
)
我检查了自动生成的映射:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="App.Models.Entities.Unit, App, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Unit`">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<discriminator type="String">
<column name="TypeId" />
</discriminator>
<property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<subclass name="App.Models.Entities.Employee, App, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<bag access="backfield" name="Groups" table="GroupToEmployee">
<key>
<column name="Employee_id" />
</key>
<many-to-many class="App.Models.Entities.Group, App, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Group_id" />
</many-to-many>
</bag>
</subclass>
<subclass name="App.Models.Entities.Group, App, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<bag access="backfield" inverse="true" name="Employees" table="EmployeeToGroup">
<key>
<column name="Group_id" />
</key>
<many-to-many class="App.Models.Entities.Employee, App, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Employee_id" />
</many-to-many>
</bag>
<property name="EmployeesCount" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="EmployeesCount" />
</property>
</subclass>
</class>
</hibernate-mapping>
嗯..没有公式......当我试图从组i中获取所有记录时,我得到一个例外:
SQLite error
no such column: group0_.EmployeesCount
生成的查询错误:
NHibernate: select group0_.Id as Id6_, group0_.Name as Name6_, **group0_.EmployeesCount** as Employee9_6_ from "Unit" group0_ where group0_.TypeId='App.Models.Entities
.Group'
怎么了?它应该有效吗?
答案 0 :(得分:1)
您的映射覆盖似乎未被考虑在内。如果是,你应该在映射文件中看到类似的内容:
<property name="EmployeesCount" formula="count(*)" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
在构建如下的映射时,是否包含了映射覆盖:
“要使用覆盖,您需要指示您的AutoMap实例使用它们。通常这将在流畅的配置设置的上下文中完成,但我只是用它自己的AutoMap来说明。”
AutoMap.AssemblyOf<Person>(cfg)
.UseOverridesFromAssemblyOf<PersonMappingOverride>();
上述内容取自http://wiki.fluentnhibernate.org/Auto_mapping#Overrides