INamingStrategy 被(流畅的)NHibernate 忽略?
我正在尝试为 NHibernate 编写一个命名策略,该策略将根据定义 poco 的程序集为表名称添加前缀。现在我的策略只是尝试将任何前缀附加到表中,以证明我的连接正确。
我遇到的问题是我能够创建 INamingStrategy 并将其附加到 NHibernate 配置对象,但它似乎从未被使用过。下面是一些编码示例:
private MsSqlConfiguration GetDatabaseConfiguration()
{
var configuration = MsSqlConfiguration.MsSql2008
.ConnectionString(ConfigFileReader.GetConnectionString(ConnectionStringKey))
.ShowSql();
return configuration;
}
private FluentConfiguration GetFluentConfiguration()
{
return Fluently.Configure().Database(GetDatabaseConfiguration())
.Mappings(m =>
{
foreach (var assembly in GetAssembliesToLoadMappingsFrom())
m.FluentMappings.AddFromAssembly(assembly);
});
}
public global::NHibernate.Cfg.Configuration GetNHibernateConfiguration()
{
var nHibernateConfiguration = GetFluentConfiguration().BuildConfiguration();
var namingStrategy = GetNamingStrategy();
if (namingStrategy != null)
nHibernateConfiguration.SetNamingStrategy(namingStrategy);
return nHibernateConfiguration;
}
public void Build()
{
var schemaExport = new SchemaExport(GetNHibernateConfiguration());
schemaExport.Create(true, true);
}
通过在 GetNHibernateConfiguration() 中的 return 语句上放置断点,我能够确认 nHibernateConfiguration.NamingStrategy 包含对我的策略的引用。然而,在该策略的每个 INamingStrategy 实现成员中放置断点表明它们都没有被调用。通过查看生成的模式可以确认这一点,该模式没有前缀。
同样,使用相同的方法创建会话工厂,表明 CRUD 操作也忽略该策略。
我错过了一些明显的东西吗?
我正在使用 NHibernate 2.1.1.4000
I am trying to write a naming strategy for NHibernate that will prefix table names based on what assembly the poco is defined in. Right now my strategy is just trying to append any prefix at all to the tables just to prove I have things wired up right.
The problem that I am encountering is that I am able to create my INamingStrategy and attach it to the NHibernate configuration object, but it never seems to get used. Here is some example coded:
private MsSqlConfiguration GetDatabaseConfiguration()
{
var configuration = MsSqlConfiguration.MsSql2008
.ConnectionString(ConfigFileReader.GetConnectionString(ConnectionStringKey))
.ShowSql();
return configuration;
}
private FluentConfiguration GetFluentConfiguration()
{
return Fluently.Configure().Database(GetDatabaseConfiguration())
.Mappings(m =>
{
foreach (var assembly in GetAssembliesToLoadMappingsFrom())
m.FluentMappings.AddFromAssembly(assembly);
});
}
public global::NHibernate.Cfg.Configuration GetNHibernateConfiguration()
{
var nHibernateConfiguration = GetFluentConfiguration().BuildConfiguration();
var namingStrategy = GetNamingStrategy();
if (namingStrategy != null)
nHibernateConfiguration.SetNamingStrategy(namingStrategy);
return nHibernateConfiguration;
}
public void Build()
{
var schemaExport = new SchemaExport(GetNHibernateConfiguration());
schemaExport.Create(true, true);
}
By placing a breakpoint on the return statement in GetNHibernateConfiguration(), I am able to confirm that nHibernateConfiguration.NamingStrategy contains a reference to my strategy. However, placing breakpoints in every one of the INamingStrategy implementing members of that strategy shows that non of them are ever called. This is confirmed by looking at the generated schema, which has no prefixes.
Likewise, using the same approach to create a session factory, shows that CRUD operations also ignore the strategy.
I am I missing something obvious?
I am using NHibernate 2.1.1.4000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的策略太复杂了。如果您使用的是 FluentNHibertate,只需在初始化中提供 TableName 约定即可。
eq:
和用法在这里:
I think your strategy is too complicated. If you are using FluentNHibertate just provide the TableName convention into your initialization.
e.q.:
and usage here: