努力理解nHibernate SchemaUpdate,即使使用Blog Posts也是如此

时间:2011-07-16 14:32:53

标签: nhibernate fluent-nhibernate schema

我已经看过关于nHibernate的SchemaUpdate甚至Ayende's非常好的例子的各种博文,并且下载了样本,但由于某些原因我不能为我工作同样的事情。我会注意到我正在使用Fluent NHibernate,但据我所知,这不应该产生太大的差异。

更新

  

我已经达到SchemaUpdate运行的程度,但它只是一个完整的模式创建,没有'改变'。换句话说,就像我刚建立数据库一样。我在下面发布我的完整资料。

这是我基本上尝试的...我认为它通常是非常明显的,但基本上我使用Fluent配置创建一个Configuration对象,然后尝试将其传入。单元测试通过,程序运行......但实际上并没有发生。我永远看不到任何结果,我永远不会看到数据库架构得到更新。

  

创建数据库(缺少列等)

     

然后,数据库将在下次运行时使用新架构进行映射。

     

数据库(更新)应该根据Update方法更新Schema。

但这不是实际发生的事情。

我还查看了有关此事的其他帖子。就像这里:http://morten.lyhr.dk/2008/03/nhibernates-schemaupdate-feature.html

此外,我在找到以下Stack Overflow帖子后更改了我的代码 Make Fluent NHibernate output schema update to file

即使示例代码无法使这个功能正面或反面。

代码

    private static void UpdateSchema(NHibernate.Cfg.Configuration Config) {
        System.Action<string> updateExport = x => {
            using (var file = new System.IO.FileStream(@"C:\Users\User\Documents\Visual Studio 2010\Mappings\update.sql", System.IO.FileMode.Append, System.IO.FileAccess.Write))
            using (var sw = new System.IO.StreamWriter(file)) {
                sw.Write(x);
                sw.Close();
            }
        };
        NHibernate.Tool.hbm2ddl.SchemaUpdate SchemaUpdater = new NHibernate.Tool.hbm2ddl.SchemaUpdate(Config);
        SchemaUpdater.Execute(updateExport, false);
    }

    public static ISessionFactory Map(string connectionString) {
        // fluently configure an ms-sql 2008 database
        return FluentNHibernate.Cfg.Fluently.Configure()
            .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                      .ConnectionString(c => c.Is(connectionString))
                      .AdoNetBatchSize(50)
                      .FormatSql()
                      .UseReflectionOptimizer())
            .Cache(c => c
                   .ProviderClass<NHibernate.Caches.SysCache2.SysCacheProvider>()
                   .UseQueryCache()
                   .UseSecondLevelCache()
                   .UseMinimalPuts())
            .Mappings(m => {
                m.FluentMappings.Conventions.Setup(x => {
                        x.AddFromAssemblyOf<Mappings.AspectMap>();
                        x.Add<EnumConvention>();
                        x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Never());
                    });
                m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly());
            })
            .ExposeConfiguration(UpdateSchema)
            .BuildSessionFactory();
    }

1 个答案:

答案 0 :(得分:5)

你的例子适合我(NH3.1,FNH 2.1)。 SchemaUpdater检查当前数据库模式并创建正确的alter脚本。所有生成的脚本片段都将公开给UpdateSchema。这里没问题。我感到困惑的唯一一个细节是文件模式访问:

  1. 第一个地图运行:整个数据库模式脚本是 存储在update.sql文件中。请注意,脚本尚未执行,因为doUpdate参数为false:SchemaUpdater.Execute(updateExport, false);
  2. 手动执行update.sql文件 - 创建数据库模式。
  3. 更改FNH映射(例如添加属性)。
  4. 第二个地图运行:Corretct alter table脚本追加到update.sql文件。
  5. 在架构更新之前删除输出文件可能会更好。

    生成的脚本可以自动执行(doUpdate参数为true):

    SchemaUpdater.Execute(updateExport, true);
    

    我不知道是不是你的情况,但我现在很开心 - 你的代码片段解决了我的一些问题。感谢您的灵感:)。

相关问题