NHibernate 可以检查数据库模式是否已生成吗?

发布于 2024-07-24 17:53:24 字数 682 浏览 6 评论 0原文

所以,NHibernate 新手用户; 试图让我的大脑围绕它。

我正在考虑如何处理部署,以及稍后将附加组件注入到 Web 应用程序(这可能需要它们自己的持久性类)。

我认为使用 SchemaExport 进行部署效果会很好,但我想知道是否有办法让 NHibernate 以一种常见的、基于代码的方式告诉我模式导出已完成已经,或者还没有。 基本上,我想做这样的伪代码:

  if(!_cfg.HasSchemaForType(typeof(MyType))
       ExportSchema(typeof(MyType));
  else
       UpdateSchema(typeof(MyType));

其中两个函数将在内部分别使用 SchemaExportSchemaUpdate


编辑:伙计们,我很欣赏到目前为止的答案,但他们有点忽略了重点。 我试图设置的是应用程序允许添加和删除可能需要更改数据库的附加组件的方法。 我不是在谈论对我自己的代码等进行版本控制(至少不是其主要功能)。 因此,问题不在于我何时部署应用程序,而更多在于我何时添加或删除插件。 之前是否部署过 theis 插件(因此是伪代码类型检查)? 如果是这样,请运行更新。 如果没有,请运行导出。 合理?

So, newbie NHibernate user; trying to wrap my brain around it.

I'm contemplating how to handle deployment, and later injection of add-ons to a web app (which may require their own persistence classes).

I was thinking that using SchemaExport for the deployment would work pretty well, but I was wondering if there's a way too get NHibernate to tell me in a common, code-based way that a schema export has been done already, or not. Basically, I want to do smething like in this pseudocode:

  if(!_cfg.HasSchemaForType(typeof(MyType))
       ExportSchema(typeof(MyType));
  else
       UpdateSchema(typeof(MyType));

where the two functions would internally use SchemaExport or SchemaUpdate, respectively.


EDIT: Guys, I appreciate the answer so far, but they're missing the point a bit. What I'm trying to set up is a way for the application to allow for the addition and removal of add-ons which may require changes to the db. I'm not talking about versioning my own code or the like (at least, not as its primary function). So the question is less about when I deploy the app, and more about when I add or remove a plug-in. Has theis plugin (hence the pseudo-code type check) been deployed before? If so, run the update. If not, run the export. Make sense?

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

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

发布评论

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

评论(5

多像笑话 2024-07-31 17:53:24

我认为您正在寻找的是 SchemaUpdate.Execute 而不是使用 SchemaExportSchemaUpdate 将创建架构(如果尚不存在),或者根据需要和需要进行更新。

这对我使用 MSSQL 和 SQLite 都有效。

new SchemaUpdate(config).Execute(false, true);

I think that what you are looking for is SchemaUpdate.Execute instead of using SchemaExport. SchemaUpdate will create the schema if it doesn't already exist, or update it if required and desired.

That works for me using both MSSQL and SQLite.

new SchemaUpdate(config).Execute(false, true);
睡美人的小仙女 2024-07-31 17:53:24

是的,至少在 3.0 中

public static bool ValidateSchema()
{
    NHibernate.Tool.hbm2ddl.SchemaValidator myvalidator = new NHibernate.Tool.hbm2ddl.SchemaValidator(m_cfg);
    try
    {
        myvalidator.Validate();
        myvalidator = null;
        return true;
    }
    catch (Exception ex)
    {
        MsgBox(ex.Message, "Schema validation error");
    }
    finally
    {
        myvalidator = null;
    }

    return false;
}

对于更新部分来说,是这样。

public static void UpdateSchema()
{
    NHibernate.Tool.hbm2ddl.SchemaUpdate schema = new NHibernate.Tool.hbm2ddl.SchemaUpdate(m_cfg);
    schema.Execute(false, true);
    schema = null;
} // UpdateSchema

Yes there is, in 3.0 at least

public static bool ValidateSchema()
{
    NHibernate.Tool.hbm2ddl.SchemaValidator myvalidator = new NHibernate.Tool.hbm2ddl.SchemaValidator(m_cfg);
    try
    {
        myvalidator.Validate();
        myvalidator = null;
        return true;
    }
    catch (Exception ex)
    {
        MsgBox(ex.Message, "Schema validation error");
    }
    finally
    {
        myvalidator = null;
    }

    return false;
}

For the update part, do.

public static void UpdateSchema()
{
    NHibernate.Tool.hbm2ddl.SchemaUpdate schema = new NHibernate.Tool.hbm2ddl.SchemaUpdate(m_cfg);
    schema.Execute(false, true);
    schema = null;
} // UpdateSchema
落叶缤纷 2024-07-31 17:53:24

不,NHibernate 不会执行您所要求的操作。 我想可以编写一些导出模式的代码,然后将其与数据库模式进行比较。 但导出到临时数据库并使用第三方工具(例如 redgate SQL Compare)来比较架构可能会更容易。

即使它满足了您的要求,我也不知道这对部署有什么帮助,因为它的目的是从头开始创建数据库。

编辑添加:假设每个插件都有自己的一组表,您可以确定是否已使用以下几种方法之一部署架构:

  • 尝试加载其中一个插件对象并捕获异常。
  • 检查数据库架构(使用 SMO for SQL Server)以检查表是否存在。
  • 部署插件时在表中创建一条记录。

No, NHibernate doesn't do what you're asking. I imagine it would be possible to write some code that exported the schema and then compared it to the database schema. But it would probably be easier to export into a temporary database and use a 3rd party tool, such as redgate SQL Compare, to compare the schemas.

Even if it did what you're asking, I don't see how that would help with deployment because its purpose is to create a database from scratch.

Edited to add: Assuming each plugin has its own set of tables, you could determine if the schema has been deployed using one of several methods:

  • Attempt to load one of the plugin objects and catch the exception.
  • Examine the database schema (using SMO for SQL Server) to check if the table(s) exist.
  • Create a record in a table when a plugin is deployed.
单调的奢华 2024-07-31 17:53:24

模式导出的目的是从头开始生成完整的模式。 如果您尚未部署应用程序,这非常有用。

第一次部署后,我强烈建议使用迁移工具,它将帮助您进一步扩展/修改架构。 如果您多想一点,您会发现随着应用程序的发展,您甚至需要数据操作(例如删除由于错误而生成的错误数据)。 这就是迁移工具可以帮助您的全部内容。

查看:

以下是更多适用于 .net 的迁移工具的列表在一个SO问题中回答:

迁移的最初想法源于 Ruby on Rails,并且已经过去被“克隆”到其他框架中。 这就是为什么在 http://guides.rubyonrails.org/migrations 上阅读原始想法绝对是件好事。 html 也是如此。

The purpose of schema export is to generate the complete schema from scratch. Really useful if you haven't deployed your application yet.

After the first deployment I would highly recommend using a migrations tool which will help you with further extensions/modifications of the schema. If you think a bit more ahead you will notice that you even require data manipulation (e.g. removing wrong data which has been generated due to a bug) as your application evolves. That's all a migration tool can help you with.

Take a look into:

Here is a list of more migration tools for .net answered in a SO question:

The original idea of migrations originated from Ruby on Rails and has been "cloned" into other frameworks over the past. That's why it's definitely good to read about the original idea at http://guides.rubyonrails.org/migrations.html too.

忆梦 2024-07-31 17:53:24

如果您有 VS Team Suite 或数据库开发人员版本,它可以同步和跟踪更改,然后创建一个部署脚本来为您创建所有正确的对象。 如果我没记错的话,RedGate 还有一个 Schema Compare 产品可以做同样的事情。

If you have VS Team Suite or the Database Developer edition, it can sync and track changes and then make a deployment script that will create all the right objects for you. Also RedGate has a Schema Compare product that does the same thing if I'm not mistaken.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文