Struggling to Comprehend nHibernate SchemaUpdate, even with Blog Posts

Deadly 提交于 2020-01-12 05:30:29

问题


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

回答1:


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:).



来源:https://stackoverflow.com/questions/6718025/struggling-to-comprehend-nhibernate-schemaupdate-even-with-blog-posts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!