gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:47:18 27 4
gpt4 key购买 nike

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

最佳答案

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

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

关于mysql - NHibernate 数据库创建后如何将固定数据插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21257545/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com