Make Fluent NHibernate output schema update to file

ぃ、小莉子 提交于 2019-12-04 18:37:17

问题


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.


回答1:


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.




回答2:


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);
}


来源:https://stackoverflow.com/questions/2483424/make-fluent-nhibernate-output-schema-update-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!