Я обнаружил, что доменные классы должны быть в отдельной сборке, а не в коде, который создает ISessionFactory. Как только вы переместите ваши доменные объекты в отдельную сборку, все должно работать просто отлично.
Из любопытства я попробовал Конвенции. Добавьте идею сверху, и она никогда не работает. Это означает, что я могу взять класс домена, например:
public class Person
{
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
С помощью процедуры AutoMapping, например ниже:
public NHibernate.ISessionFactory Create()
{
var persistenceModel =
AutoMap
.AssemblyOf<Person>();
var ret =
Fluently
.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().FormatSql().ConnectionString(ex => ex.FromConnectionStringWithKey("PersonSqlCe")))
.Mappings(x => x.AutoMappings.Add(persistenceModel))
.ExposeConfiguration(config =>
{
new SchemaExport(config).Create(true, true);
// DOC: workaround for identity column failures in SQLCE
config.SetProperty("connection.release_mode", "on_close");
})
.BuildSessionFactory();
return ret;
}
И все работает просто отлично. ОДНАКО, когда я изменяю свой класс домена, чтобы он выглядел так:
public class Person
{
public virtual int BAD { get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
И измените мою процедуру AutoMapping так, чтобы она соответствовала ожидаемому имени нового свойства PrimaryKey, такому как код ниже:
public NHibernate.ISessionFactory Create()
{
var persistenceModel =
AutoMap
.AssemblyOf<Person>();
persistenceModel.Conventions.Add(PrimaryKey.Name.Is(x => "BAD"));
var ret =
Fluently
.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().FormatSql().ConnectionString(ex => ex.FromConnectionStringWithKey("PersonSqlCe")))
.Mappings(x => x.AutoMappings.Add(persistenceModel))
.ExposeConfiguration(config =>
{
new SchemaExport(config).Create(true, true);
// DOC: workaround for identity column failures in SQLCE
config.SetProperty("connection.release_mode", "on_close");
})
.BuildSessionFactory();
return ret;
}
... Я получаю ошибки, подобные приведенным ниже:
----> FluentNHibernate.Visitors.ValidationException : The entity 'Person' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
SampleImplementation.cs(28,0): at NHQS.Tests.PersonSampleSessionFactoryCreator.Create()
SessionFactoryTests.cs(16,0): at
Что дает? Кажется, конвенционный подход не подключен?