soupy1976
soupy1976

Reputation: 2845

Issue using NHibernate SchemaUpdate with MySQL - no error, but nothing happens!

I've been having some issues using SchemaUpdate with MySQL.

I seem to have implemented everything correctly, but when I run it it doesn't update anything. It doesn't generate any errors, and it pauses for about the sort of length of time you would expect it to take to inspect the DB schema, but it simply doesn't update anything, and when I try to get it to script the change it just doesn't do anything - it's as if it can;'t detect any changes to up the DB schema, but I have created a new entity and a new mapping class - so I cant see why it's not picking it up.

       var config = Fluently.Configure()
               .Database(() => {
                   var dbConfig = MySQLConfiguration.Standard.ConnectionString(
                    c => c.Server(configuration.Get<string>("server", ""))
                        .Database(configuration.Get<string>("database",""))
                        .Password(configuration.Get<string>("password", ""))
                        .Username(configuration.Get<string>("user", ""))
                   );


               });

       config.Mappings(
           m => m.FluentMappings
                   .AddFromAssemblyOf<User>()
                   .AddFromAssemblyOf<UserMap>()
                   .Conventions.AddFromAssemblyOf<UserMap>()
                   .Conventions.AddFromAssemblyOf<PrimaryKeyIdConvention>()
           //      .PersistenceModel.Add(new CultureFilter())
               );


       var export = new SchemaUpdate(config);
       export.Execute(false, true);

I don't think there's anything wrong with my config because it works perfectly well with ShemaExport - it's just SchemaUpdate where I seem to have a problem.

any ideas would be much appreciated!

Upvotes: 3

Views: 731

Answers (2)

Algoman
Algoman

Reputation: 2027

I figured it out:

The problem is that MySQL doesn't have multiple databases. It seems like some parts of MySQL and/or NHibernate use Schemas instead and SchemaUpdate seems to be one of them. So when I have

Database=A

in my connectionstring, and

<class ... schema="B">

in the mapping, then SchemaUpdate seems to think that this class is "for a different database" and doesn't update it.

The only fix I can think of right now would be to do a SchemaUpdate for every single schema (calling USE schema; first). But afaik, NHibernate has no interface to get a list of all schemas that are used in the mappings (correct me if I'm wrong). I'm afraid I have to iterate through the XML files manually (I use XML-based mappings) and collect them...

Upvotes: 0

Buthrakaur
Buthrakaur

Reputation: 1851

Did you try to wrap SchemaUpdate execution in a transaction? There're some databases which need to run this in a transaction AFAIK.

using (var tx = session.BeginTransaction())
{
    var tempFileName = Path.GetTempFileName();
    try
    {
        using (var str = new StreamWriter(tempFileName))
        {
            new SchemaExport(configuration).Execute(showBuildScript, true, false, session.Connection, str);
        }
    }
    finally
    {
        if (File.Exists(tempFileName))
        {
            File.Delete(tempFileName);
        }
    }

    tx.Commit();
}

Upvotes: 1

Related Questions