INamingStrategy 被(流畅的)NHibernate 忽略?

发布于 2024-09-04 01:40:09 字数 1444 浏览 4 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-09-11 01:40:09

我认为你的策略太复杂了。如果您使用的是 FluentNHibertate,只需在初始化中提供 TableName 约定即可。

eq:

public class TableNameConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
    }
}

和用法在这里:

public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
    {
        /// <summary>
        /// Get Conf Setup
        /// </summary>
        /// <returns>
        /// Action of AutoMappingExpressions
        /// </returns>
        private Action<AutoMappingExpressions> GetSetup()
        {
            return c =>
                {
                    c.FindIdentity = type => type.Name == "Id";
                    c.IsBaseType = this.IsBaseTypeConvention;
                };
        }

        private  Action<IConventionFinder> GetConventions() 
        {
            return c =>
                { 
                    c.Add<PrimaryKeyConvention>();
                    c.Add<ReferenceConvention>();
                    c.Add<HasManyConvention>();
                    c.Add<TableNameConvention>();
                    c.Add<PropertyNameConvention>();
                };
        }

        public AutoPersistenceModel Generate()
        {
            var model =
                new AutoPersistenceModel()
                    .AddEntityAssembly(Assembly.GetAssembly(typeof(User)))
                    .Where(
                    this.GetAutoMappingFilter).Conventions.Setup(this.GetConventions()).Setup(this.GetSetup()).
                    UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
            return model;
        }

I think your strategy is too complicated. If you are using FluentNHibertate just provide the TableName convention into your initialization.

e.q.:

public class TableNameConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
    }
}

and usage here:

public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
    {
        /// <summary>
        /// Get Conf Setup
        /// </summary>
        /// <returns>
        /// Action of AutoMappingExpressions
        /// </returns>
        private Action<AutoMappingExpressions> GetSetup()
        {
            return c =>
                {
                    c.FindIdentity = type => type.Name == "Id";
                    c.IsBaseType = this.IsBaseTypeConvention;
                };
        }

        private  Action<IConventionFinder> GetConventions() 
        {
            return c =>
                { 
                    c.Add<PrimaryKeyConvention>();
                    c.Add<ReferenceConvention>();
                    c.Add<HasManyConvention>();
                    c.Add<TableNameConvention>();
                    c.Add<PropertyNameConvention>();
                };
        }

        public AutoPersistenceModel Generate()
        {
            var model =
                new AutoPersistenceModel()
                    .AddEntityAssembly(Assembly.GetAssembly(typeof(User)))
                    .Where(
                    this.GetAutoMappingFilter).Conventions.Setup(this.GetConventions()).Setup(this.GetSetup()).
                    UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
            return model;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文