NHibernate 可以检查数据库模式是否已生成吗?
所以,NHibernate 新手用户; 试图让我的大脑围绕它。
我正在考虑如何处理部署,以及稍后将附加组件注入到 Web 应用程序(这可能需要它们自己的持久性类)。
我认为使用 SchemaExport
进行部署效果会很好,但我想知道是否有办法让 NHibernate 以一种常见的、基于代码的方式告诉我模式导出已完成已经,或者还没有。 基本上,我想做这样的伪代码:
if(!_cfg.HasSchemaForType(typeof(MyType))
ExportSchema(typeof(MyType));
else
UpdateSchema(typeof(MyType));
其中两个函数将在内部分别使用 SchemaExport
或 SchemaUpdate
。
编辑:伙计们,我很欣赏到目前为止的答案,但他们有点忽略了重点。 我试图设置的是应用程序允许添加和删除可能需要更改数据库的附加组件的方法。 我不是在谈论对我自己的代码等进行版本控制(至少不是其主要功能)。 因此,问题不在于我何时部署应用程序,而更多在于我何时添加或删除插件。 之前是否部署过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找的是
SchemaUpdate.Execute
而不是使用SchemaExport
。SchemaUpdate
将创建架构(如果尚不存在),或者根据需要和需要进行更新。这对我使用 MSSQL 和 SQLite 都有效。
I think that what you are looking for is
SchemaUpdate.Execute
instead of usingSchemaExport
.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.
是的,至少在 3.0 中
对于更新部分来说,是这样。
Yes there is, in 3.0 at least
For the update part, do.
不,NHibernate 不会执行您所要求的操作。 我想可以编写一些导出模式的代码,然后将其与数据库模式进行比较。 但导出到临时数据库并使用第三方工具(例如 redgate SQL Compare)来比较架构可能会更容易。
即使它满足了您的要求,我也不知道这对部署有什么帮助,因为它的目的是从头开始创建数据库。
编辑添加:假设每个插件都有自己的一组表,您可以确定是否已使用以下几种方法之一部署架构:
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:
模式导出的目的是从头开始生成完整的模式。 如果您尚未部署应用程序,这非常有用。
第一次部署后,我强烈建议使用迁移工具,它将帮助您进一步扩展/修改架构。 如果您多想一点,您会发现随着应用程序的发展,您甚至需要数据操作(例如删除由于错误而生成的错误数据)。 这就是迁移工具可以帮助您的全部内容。
查看:
以下是更多适用于 .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.
如果您有 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.