首页 文章

NHibernate定义模式名称

提问于
浏览
1

我正在努力学习nhibernate . 我理解如何创建表和实体映射 . 但我无法理解的是如何使用nHibernate创建一个新的Schema .

我搜索并发现使用“ExposeConfiguration”你可以创建一个Schema,但我无法理解我在哪里提供我的新Schema名称 .

1 回答

  • 0

    有非常明确的文件:

    因此,Schema名称基本上可以设置每个类/表:

    public MyEntityMap()
    {
        ...
        Schema("mySchema");
    }
    

    默认可以像这样设置:

    MsSqlConfiguration.MsSql2005
      .UseOuterJoin()
      .DefaultSchema("dbo");
    

    这也是一代人:

    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>
    

    如果这个脚本首先包含在流畅的配置中,它将为我们创建架构:

相关问题