NHibernate db创建后如何将固定数据插入数据库?

时间:2014-01-21 12:00:24

标签: mysql c#-4.0 nhibernate fluent-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;
}

1 个答案:

答案 0 :(得分: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