【问题标题】:How to insert fixed data into database after NHibernate db created?NHibernate db创建后如何将固定数据插入数据库?
【发布时间】:2014-01-21 12:00:23
【问题描述】:

我想在创建数据库后将固定数据插入到表中(如城市、城镇、工作、部门等)。我想我需要一个 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;
}

【问题讨论】:

    标签: mysql c#-4.0 nhibernate fluent-nhibernate


    【解决方案1】:

    创建数据库后,我不知道如何处理会话。我现在想通了,解决方案是这样的:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      相关资源
      最近更新 更多