是否可以设置 SQL Server 列?使用流畅的 nhibernate 地图进行描述?
我正在使用流畅的 nhibernate 映射来生成 MS SQL Server 数据库。
我希望能够将列的描述设置为这一代的一部分。
I am using my fluent nhibernate mappings to generate my MS SQL Server database.
I would like to be able to set a columns' description as part of this generation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这是不可能的。描述是 Microsoft 特定的元数据属性,NHibernate 和 Fluent NHibernate 都尽可能保持与数据库无关。
您也许可以使用
SqlInsert
映射来完成此操作,但这不会很好。No, it isn't possible. Description is a Microsoft specific meta-data attribute, and both NHibernate and Fluent NHibernate try to remain as database agnostic as possible.
You might be able to do it using a
SqlInsert
mapping, but it won't be very nice.这也不是不可能,但是 Fluent NHibernate 和 NHibernate 本身都没有提供这样的 API。
在 NHibernate 创建数据库后,您必须编写一些代码来检查生成的数据库结构,然后编写良好的旧 SQL 语句来自己设置描述。
也许您可以创建一个 Fluent NHibernate“约定”来记录创建的表以及其中的列,然后您可以轻松编写手动设置描述的代码。
(如果您需要,我会为您编写一些代码。)
It is not impossible, but there is no such API provided by Fluent NHibernate nor NHibernate itself.
You'd have to write some code to examine the resulting database structure after NHibernate created your database and then write good old SQL statements to set the descriptions yourself.
Perhaps you could create a Fluent NHibernate "convention" which logs what tables are created and what columns are there and then you could easily write the code that sets the descriptions manually.
(I'll write some code for you, if you require.)
如果您确实需要这个,您可以将列描述映射到“holder”表,该表将存储用于保存/加载的描述。该持有者表将需要一个包含 SchemaName、TableName、ColumnName 和 Description 的列。在此表上添加一个触发器,这将执行 SQL Server 的 sp_addextendedproperty (Transact-SQL) 或 sp_dropextendedproperty (Transact-SQL) 命令添加/将描述删除到正确的 schema.table.column 或从正确的 schema.table.column 删除描述。
表将如下所示:
触发器将如下所示:
如果您担心表与实际列描述不同步,您可以将其添加到触发器的末尾:
此代码将回滚并中止触发器,如果DescriptionHolder 与数据库中的实际列描述不 100% 同步。
If you really need this, you could map the columns descriptions to a "holder" table which would store the descriptions for saving/loading. This holder table will need a column for SchemaName, TableName, ColumnName, and Description. Add a TRIGGER on this table, which will execute SQL Server's sp_addextendedproperty (Transact-SQL) or sp_dropextendedproperty (Transact-SQL) command to add/drop the description to/from the proper schema.table.column.
the table would be like:
the trigger would be like:
If you're worried about getting the table out of sync with the actual column descriptions, you could add this onto the end of the trigger:
this code will rollback and abort the trigger if the DescriptionHolder is not 100% in sync with the actual column descriptions in the database.