2014-01-21 141 views
0

我想在創建數據庫之後將固定數據插入表(如城市,城鎮,工作,部門等)。我想我需要一個NHibernate的助手類來做到這一點,但如何?如何在NHibernate數據庫創建後將固定數據插入數據庫?

public static ISessionFactory CreateSessionFactory() 
{ 
    IPersistenceConfigurer dbConfigurer = MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("test_nh")); 

    var fConfig = Fluently.Configure() 
     .Database(dbConfigurer) 
     .Mappings(m => m.FluentMappings 
      .AddFromAssemblyOf<Adres>() 
      ) 
     .ExposeConfiguration(cfg => 
           { 
            SchemaExport config = new SchemaExport(cfg).Create(true, true); 
// I think code will be here 
// There is file which contains sql script to insert data 
           }) 
     .BuildSessionFactory(); 

    return fConfig; 
} 

回答

0

db創建後,我不知道如何處理會話。我現在想出來了,解決方案是這樣的:

public static ISessionFactory CreateSessionFactory() 
{ 
    IPersistenceConfigurer dbConfigurer = MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("test_nh")); 

    var fConfig = Fluently.Configure() 
     .Database(dbConfigurer) 
     .Mappings(m => m.FluentMappings 
      .AddFromAssemblyOf<Adres>() 
      ) 
     .ExposeConfiguration(cfg => 
           { 
            SchemaExport config = new SchemaExport(cfg); 
            config.Create(true, true); 

            var ses1 = cfg.BuildSessionFactory().OpenSession(); 
            TextReader tr = new StreamReader(@"P:\cbo\App_Code\FixedSQL\Countries.sql"); 
            string sql = tr.ReadToEnd(); 
            var ulkeler= ses1.CreateSQLQuery(sql).List(); 
           }) 
     .BuildSessionFactory(); 
    return fConfig; 
} 

PS:SQL文件大小對MySQL很重要。如果您想擴展它,您應該將此行添加到my.ini文件中:

[mysqld] 
max_allowed_packet = 32M