2010-10-28 70 views
0

我對MySQL的類配置器功能NHibernate:MySQL配置的功能NHibernate

class MySqlInitializer : INHibernateInitializer 
{ 
    public Configuration GetConfiguration() 
    { 
     var dbServer = "localhost"; 
     var dbUsername = "root"; 
     var dbName = "nhibernate"; 
     var dbPassword = ""; 
     var ormAssembly = "NHibernate_MySQL.Domain"; 

     var config = Fluently.Configure() 
      .Database(MySQLConfiguration 
          .Standard 
          .ConnectionString(cs => cs 
                 .Server(dbServer) 
                 .Database(dbName) 
                 .Username(dbUsername) 
                 .Password(dbPassword))) 
      .Mappings(
       x => 
       x.FluentMappings.AddFromAssembly(Assembly.Load(ormAssembly))); 

     return config.BuildConfiguration(); 
    } 
} 

所有數據的正確。 所以,我也有集成測試(的xUnit):

[Fact] 
    public void GenerateMigrationScript() 
    { 
     var config = new MySqlInitializer().GetConfiguration(); 
     var factory = (ISessionFactoryImplementor) config.BuildSessionFactory(); 

     using (var session = factory.OpenSession()) 
     { 
      var updateScripts = config 
       .GenerateSchemaUpdateScript(factory.Dialect, 
              new DatabaseMetadata((DbConnection) session.Connection, factory.Dialect)); 

      foreach (var updateScript in updateScripts) 
      { 
       Console.WriteLine(updateScript + ";"); 
      } 
     } 
    } 

我的實體:

public interface IHiberEntutyBase 
{ 
    Guid Id { get; set; } 
} 

public class HiberEntity : IHiberEntutyBase 
{ 
    public virtual Guid Id { get; set; } 

    public virtual string Name { get; set; } 
} 

class HiberEntityMap : ClassMap<HiberEntity> 
{ 
    public HiberEntityMap() 
    { 
     Id(x => x.Id).GeneratedBy.Guid(); 

     Map(x => x.Name); 
    } 
} 

當我運行GenerateMigrationScript()寫道:「MySql.Data.MySqlClient.MySqlException:未知數據庫 'NHibernate的' 」。 請幫助我。

回答

3

它意味着它說:你沒有一個名爲「nhibernate」的數據庫。

NHibernate不創建數據庫,只有模式(表,鍵)。數據庫必須存在。