private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard
.UsingFile("firstProject.db")
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
// delete the existing db on each run
if (File.Exists(DbFile))
File.Delete(DbFile);
// this NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it
new SchemaExport(config)
.Create(false, true);
}
EXTEND:如何创建架构本身? (例如 mySchemaName )
使用HBM,我们可以介绍 embedded resource :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyNamespace" assembly="MyAssembly">
<database-object>
<create >
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'mySchemaName')
EXEC( 'CREATE SCHEMA mySchemaName' )
GO
</create>
<drop></drop>
</database-object>
</hibernate-mapping>
1 回答
有非常明确的文件:
Database configuration
Schema generation
因此,Schema名称基本上可以设置每个类/表:
默认可以像这样设置:
这也是一代人:
EXTEND:如何创建架构本身? (例如
mySchemaName
)使用HBM,我们可以介绍 embedded resource :
如果这个脚本首先包含在流畅的配置中,它将为我们创建架构: