How do I configure FluentNHibernate to not overwri

2019-02-26 20:48发布

问题:

Here is my configuration:

this.factory = Fluently.Configure().
    Database(SQLiteConfiguration.Standard.UsingFile("foo.db").
        ShowSql()).
    Mappings(m => m.FluentMappings.AddFromAssemblyOf<Bar>()).
    ExposeConfiguration(BuildSchema).
    BuildSessionFactory();

BuildSchema looks like this:

private static void BuildSchema(Configuration config)
{
    new SchemaExport(config).Create(false, true);
}

Luckily this works great and creates a file named foo.db to which I can read and write. Unluckily, every time i run this code, foo.db is overwritten. How can I configure (Fluent)NHibernate to create the file only if it doesn't already exist?

回答1:

Put an if statement in your BuildSchema?

if (!File.Exists("foo.db"))
  new SchemaExport(config).Create(false, true);