NHibernate和代码第一 [英] NHibernate and code first

查看:126
本文介绍了NHibernate和代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你在实际应用中使用SchemaExport和SchemaUpdate吗?最初,你创建模型,然后生成架构?它工作吗?通常,我使用设计器创建数据库(使用Visual Studio数据库项目),然后使用映射和持久化类或EF实体创建数据库。但现在,我想用Fluent NHibernate尝试第一个代码。



我研究了SchemaExport和SchemaUpdate,发现了一些问题。例如,update不会删除数据库对象,如果存在表,则不会创建非空列(如可为空),也不会在多对多表上生成主键等等。这意味着我必须经常重新创建数据库。但是,数据是什么?而且,如何部署更改生产数据库等等...



我想知道在应用程序中真正使用代码和SchemaExport(SchemaUpdate)吗?可能你可以给我一些建议...

解决方案

我在生产中使用SchemaUpdate。正是因为它从来不会像删除列这样的破坏性操作,所以很安全。但是,这不是更新数据库的全面解决方案。如果你使用它,你仍然需要用脚本来补充它,以便更新你的模式来完成像删除(如你所提到的),索引,改变列类型,添加表格数据等等。但SchemaUpdate覆盖了90%的情况。



我发现的唯一缺点是随着时间的推移,偶尔会在表格中添加重复的外键约束。



还有一件事:您应该从构建工具手动运行SchemaUpdate,而不是您的应用程序本身。给你的应用程序修改你的db schema的权利是不安全的


Do you use SchemaExport and SchemaUpdate in real applications? Initially, you create model and then generate schema? Does it work? Or, you use it only for tests...

Usually, I create db (using visual studio database project) and then mappings and persistent classes or EF entities using designer. But now, I want to try code first approach with Fluent NHibernate.

I have researched SchemaExport and SchemaUpdate and found some issues. For example, update doesn't delete db objects, creates not null columns like nullable if table exists, doesn't generate primary key on many-to-many tables and so on. It mean that I have to recreate db very often. But, what's about data? And, how to deploy changes to production db and so on...

I want to know do you really use code first and SchemaExport(SchemaUpdate) in your applications? May be you can give me some advices...

解决方案

I use SchemaUpdate in production. It is safe precisely because it never does destructive operations like deleting columns. However, it is not a comprehensive solution for updating your database. If you use it you will still have to supplement it with script to update your schema to do things like deleting (as you mention), indexes, changing column type, adding table data, etc. But SchemaUpdate covers the 90% case for me.

The only downside I've discovered is that over time it seems to occasionally add duplicate foreign-key constraints to my table.

One more thing: you should run SchemaUpdate manually from a build tool, not your app itself. It is not safe to give your application the rights to modify your db schema!

这篇关于NHibernate和代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆