使 Fluent NHibernate 输出模式更新到文件

发布于 2024-08-26 02:52:04 字数 920 浏览 2 评论 0原文

我成功地让 Fluent NHibernate 通过调用 UpdateBaseFiles:

Public Sub UpdateBaseFiles()
    Dim db As SQLiteConfiguration
    db = SQLiteConfiguration.Standard.UsingFile(BASE_DBNAME)
    Fluently.Configure() _
            .Database(db) _
            .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of FluentMap)()) _
            .ExposeConfiguration(AddressOf UpdateSchema) _
            .BuildConfiguration()
End Sub
Private Sub UpdateSchema(ByVal Config As Configuration)
    Dim SchemaUpdater As New SchemaUpdate(Config)
    SchemaUpdater.Execute(True, True)
End Sub

如何将 DDL 输出到文件来更新我的数据库,我在最初使用以下方法创建架构时执行此操作:

Private Sub BuildSchema(ByVal Config As Configuration)
    Dim SchemaExporter As New SchemaExport(Config)
    SchemaExporter.SetOutputFile("schema.sql")
    SchemaExporter.Create(False, True)
End Sub

但 SchemaUpdate 没有 SetOutputFile 方法。

I am successfully getting Fluent NHibernate to update my database by calling UpdateBaseFiles:

Public Sub UpdateBaseFiles()
    Dim db As SQLiteConfiguration
    db = SQLiteConfiguration.Standard.UsingFile(BASE_DBNAME)
    Fluently.Configure() _
            .Database(db) _
            .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of FluentMap)()) _
            .ExposeConfiguration(AddressOf UpdateSchema) _
            .BuildConfiguration()
End Sub
Private Sub UpdateSchema(ByVal Config As Configuration)
    Dim SchemaUpdater As New SchemaUpdate(Config)
    SchemaUpdater.Execute(True, True)
End Sub

How do I output the DDL to a file, I do this when initially creating the schema by using:

Private Sub BuildSchema(ByVal Config As Configuration)
    Dim SchemaExporter As New SchemaExport(Config)
    SchemaExporter.SetOutputFile("schema.sql")
    SchemaExporter.Create(False, True)
End Sub

but SchemaUpdate does not have a SetOutputFile method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2024-09-02 02:52:04

SchemaUpdate 具有接受 Action 委托的重载,您可以提供该委托来导出脚本。这是 C# 中的一个示例:

Action<string> updateExport = x =>
    {
        using (var file = new FileStream(path, FileMode.Create, FileAccess.Append))
        using (var sw = new StreamWriter(file))
        {
            sw.Write(x);
        }
    };
new SchemaUpdate(config).Execute(updateExport, false);

我认为这会起作用。我无法测试它,因为 SchemaUpdate 不支持 SQLCE

SchemaUpdate has an overload that accepts an Action<string> delegate that you can supply to export the script. Here's an example in C#:

Action<string> updateExport = x =>
    {
        using (var file = new FileStream(path, FileMode.Create, FileAccess.Append))
        using (var sw = new StreamWriter(file))
        {
            sw.Write(x);
        }
    };
new SchemaUpdate(config).Execute(updateExport, false);

I think that will work. I wasn't able to test it because SchemaUpdate is not working with SQLCE.

梦在深巷 2024-09-02 02:52:04

SchemaUpdate 会为其执行的每个更新调用该操作,因此您不希望在每个命令上重新创建(并因此覆盖)与上述相同的文件,这是必需的:

using (var file = new FileStream(@"..\..\..\schema-update.sql",
       FileMode.Create,
       FileAccess.ReadWrite))
using (var sw = new StreamWriter(file))
{
   new SchemaUpdate(config)
       .Execute(sw.Write, false);
}

SchemaUpdate calls the action for each update it does so you dont want to be recreating (and therefore overwriting) the same file on each command as above, this is what is required:

using (var file = new FileStream(@"..\..\..\schema-update.sql",
       FileMode.Create,
       FileAccess.ReadWrite))
using (var sw = new StreamWriter(file))
{
   new SchemaUpdate(config)
       .Execute(sw.Write, false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文