努力理解nHibernate的SchemaUpdate,即使是博客文章 [英] Struggling to Comprehend nHibernate SchemaUpdate, even with Blog Posts

查看:463
本文介绍了努力理解nHibernate的SchemaUpdate,即使是博客文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过各种关于nHibernate的 SchemaUpdate ,甚至是 Ayende的很好的例子,样品,但由于某种原因,我不能得到同样的东西为我工作。我会注意到我正在使用 Fluent NHibernate ,但从我可以告诉的不应该做太大的差别。



更新




我已到达 SchemaUpdate 运行的地步,它只是一个完整的模式创建,不会改变。换句话说,就好像我只是建立了新的数据库一样。

以下是我基本上正在尝试的内容...我认为这通常是非常明显的,但是,基本上我使用Fluent Configuration创建了一个 Configuration 对象,然后试图传入它。我永远不会看到任何结果,我永远不会看到数据库架构得到更新。


数据库被创建(缺少列等)

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

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



我也看过有关此事的其他文章。像这样: http://morten.lyhr.dk/2008/03 /nhibernates-schemaupdate-feature.html



另外,在找到以下堆栈溢出后,我更改了我的代码
让流畅的NHibernate输出模式更新到文件


$

$ b

代码


$ b即使是示例代码也无法制作此功能的正面或反面。 $ b

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

$ b $ public static ISessionFactory Map(string connectionString){
//流畅地配置ms-sql 2008数据库
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 b .UseSecondLevelCache()
.UseMinimalPuts())
.Mappings(m => {
m.FluentMappings.Conventions.Setup(x => {
x.AddFromAssemblyOf< ; Mappings.AspectMap>();
x.Add< EnumConvention&g吨;();
x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Never());
});
m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly());
))
.ExposeConfiguration(UpdateSchema)
.BuildSessionFactory();


解决方案

(NH3.1,FNH 2.1)。 SchemaUpdater检查当前数据库模式并创建正确的更改脚本。所有生成的脚本片段都暴露给UpdateSchema。这里没问题。第一个映射运行:整个数据库模式脚本是存储在
中的文件模式的唯一一个细节是文件模式访问:


  1. 在update.sql文件中。请注意,那里脚本还没有执行,因为doUpdate参数是false: SchemaUpdater.Execute(updateExport,false);

  2. 执行手动更新数据库模式 - 创建数据库模式。
  3. 更改FNH映射(例如添加一个属性)。
  4. 第二个映射运行:Corretct alter table script 追加到update.sql文件。

也许最好是将输出

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

  SchemaUpdater.Execute(updateExport,true); 

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


I've seen the various blog posts concerning nHibernate's SchemaUpdate, and even Ayende's very good example, and downloaded the samples, but for some reason I cannot get the same same thing to work for me. I will note that I am using Fluent NHibernate, but from what I can tell that should not make too huge a difference.

Update

I have reached the point where the SchemaUpdate runs, but it is just a full schema creation, no 'altering'. In otherwords, it's the same as if I just built the database fresh. I am posting my full source below.

Here Is what I am basically trying... I think it is generally self obvious, but basically I am creating a Configuration object using the Fluent Configuration, and then trying to pass it in. Unit Tests pass, programs run ...but nothing actually happens. I can never see any results, and I can never see the database schema get updated.

Database gets created (missing columns, etc)

Database then gets mapped with new schema on next run.

Database (Update) should update the Schema per the Update method.

But that isn't what is actually happening.

I also looked at other posts on the matter. Like here : http://morten.lyhr.dk/2008/03/nhibernates-schemaupdate-feature.html

Additionally, I have changed my code after finding the following Stack Overflow post Make Fluent NHibernate output schema update to file

And even with the sample code was not able to make heads or tails of this feature.

Code

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

解决方案

Your example works well for me (NH3.1, FNH 2.1). SchemaUpdater checks current database schema and creates correct alter scripts. All generated script fragments are exposed to UpdateSchema. No problem here. The only one detail which I was confused by is file mode access:

  1. First Map run: Whole database schema script is stored in update.sql file. Note that there the script is not executed yet, because doUpdate parameter is false: SchemaUpdater.Execute(updateExport, false);
  2. Execute update.sql file manually - database schema is created.
  3. Change in FNH mapping (e.g. a property is added).
  4. Second Map run: Corretct alter table script is appended to update.sql file.

Maybe it would be better to remove the output file before schema update.

The generated script can be executed automatically (doUpdate parameter is true):

SchemaUpdater.Execute(updateExport, true);

I don't know if it is your case but I'm happy now - your code snippet solved some of my problems. Thanks for inspiration:).

这篇关于努力理解nHibernate的SchemaUpdate,即使是博客文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆