Fluent NHibernate - Create database schema only if not existing

前端 未结 2 1576
野的像风
野的像风 2021-02-02 07:23

I have an application where I use Fluent Nhibernate to create my database. This far I\'ve been recreating the database schema each time. The code that does this is this:

<
2条回答
  •  无人共我
    2021-02-02 07:56

    You can just use SchemaUpdate instead, it will update the schema if it exists and create it if it does not:

    public NhibernateSessionFactory(IPersistenceConfigurer config)
    {
        _sessionFactory = Fluently.Configure().
            Database(config).
            Mappings(m => m.FluentMappings.AddFromAssemblyOf()).
            ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)).
            BuildSessionFactory();
    }
    

    One caveat: SchemaUpdate does not do destructive updates (dropping tables, columns, etc.). It will only add them.

提交回复
热议问题