سؤال

I use FluentNHibernate with AutoMapping. No custom conventions or alterations are used. Both NHibernate and FluentNHibernate assemblies are of the latest versions. Database is Sqlite3

I try to use the following entities (table per hierarchy):

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(*)");
  }
}

I auto generate schema. All is fine:

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
)

I checked out the auto generated mapping:

<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>

Hm.. No formula... and when i try to get all records from group i get an exception:

SQLite error
no such column: group0_.EmployeesCount

The generated query is wrong:

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'

What's wrong? And is it supposed to work?

هل كانت مفيدة؟

المحلول

It doesn't appear that your mapping override is being taken into account. If it was you should see something like this in the mapping file:

<property name="EmployeesCount" formula="count(*)" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Have you included your mapping overrides when building the mappings like the following:

"To use overrides, you need to instruct your AutoMap instance to use them. Typically this would be done in the context of a fluent configuration setup, but I'll just illustrate with the AutoMap on it's own."

AutoMap.AssemblyOf<Person>(cfg)
  .UseOverridesFromAssemblyOf<PersonMappingOverride>();

The above was taken from http://wiki.fluentnhibernate.org/Auto_mapping#Overrides

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top