简体   繁体   English

是否可以使用流畅的nhibernate映射设置SQL Server列的描述?

[英]Is it possible to set a SQL server columns' description using a fluent nhibernate map?

I am using my fluent nhibernate mappings to generate my MS SQL Server database. 我正在使用流畅的nhibernate映射来生成我的MS SQL Server数据库。

I would like to be able to set a columns' description as part of this generation. 我希望能够将列的描述设置为这一代的一部分。

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. 描述是Microsoft特定的元数据属性,NHibernate和Fluent NHibernate都尽可能地保持数据库不可知。

You might be able to do it using a SqlInsert mapping, but it won't be very nice. 您可以使用SqlInsert映射来完成它,但它不会很好。

It is not impossible, but there is no such API provided by Fluent NHibernate nor NHibernate itself. 这并非不可能,但Fluent NHibernate和NHibernate本身并没有提供这样的API。

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. 在NHibernate创建数据库之后,您必须编写一些代码来检查生成的数据库结构,然后编写好的旧SQL语句来自行设置描述。

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. 也许您可以创建一个Fluent NHibernate“约定”来记录创建的表以及那些列,然后您可以轻松编写手动设置描述的代码。
(I'll write some code for you, if you require.) (如果需要,我会为你写一些代码。)

If you really need this, you could map the columns descriptions to a "holder" table which would store the descriptions for saving/loading. 如果您确实需要这个,可以将列描述映射到“holder”表,该表将存储保存/加载的描述。 This holder table will need a column for SchemaName, TableName, ColumnName, and Description. 此holder表将需要SchemaName,TableName,ColumnName和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. 在此表上添加TRIGGER,它将执行SQL Server的sp_addextendedproperty(Transact-SQL)sp_dropextendedproperty(Transact-SQL)命令,以将描述添加到正确的schema.table.column中。

the table would be like: 表格如下:

DescriptionHolder
SchemaName   sysname
TableName    sysname
ColumnName   sysname
Description  varchar(7500) --or nvarchar(3750)

the trigger would be like: 触发器就像:

CREATE TRIGGER trigger_DescriptionHolder ON DescriptionHolder
   INSTEAD OF INSERT,UPDATE,DELETE
AS 
SET NOCOUNT ON


IF EXISTS (SELECT * FROM INSERTED)
BEGIN
    --handles INSERTS and UPDATEs
    --loop begin here over INSERTED
        EXECUTE sp_addextendedproperty N'MS_Description', <INSERTED.Description>
            ,N'SCHEMA' , <INSERTED.SchemaName>
            ,N'TABLE'  , <INSERTED.TableName>
            ,N'COLUMN' , <INSERTED.ColumnName>
    --loop end here


END
ELSE IF EXISTS(SELECT * FROM DELETED)
BEGIN
    --handles DELETEs
    --loop begin here over DELETED
    EXECUTE sp_dropextendedproperty ...
    --loop end here

END

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: 如果您担心表格与实际列描述不同步,可以将其添加到触发器的末尾:

IF EXISTS (SELECT 1 AS x --h.SchemaName,h.TableName,h.ColumnName
               FROM DescriptionHolder h 
               WHERE NOT EXISTS (SELECT 1  
                                     from sys.extended_properties p
                                         inner join sys.objects   o ON p.major_id=o.object_id
                                         inner join sys.schemas   s ON o.schema_id=s.schema_id
                                         inner join sys.columns   c ON o.object_id=c.object_id and p.minor_id=c.column_id
                                     where h.SchemaName=s.name AND h.TableName=o.name AND h.ColumnName=c.name)
           UNION ALL
           select 2 AS x --s.name AS SchemaName,o.name AS TableName,c.name AS ColumnName
               from sys.extended_properties p
                   inner join sys.objects   o ON p.major_id=o.object_id
                   inner join sys.schemas   s ON o.schema_id=s.schema_id
                   inner join sys.columns   c ON o.object_id=c.object_id and p.minor_id=c.column_id
               where p.class=1 and p.Name='MS_Description'
                   AND not exists (SELECT 1 FROM DescriptionHolder h WHERE s.name=h.SchemaName AND o.name=h.TableName AND c.name=h.ColumnName)
          )
    BEGIN
        RAISERROR('sys.extended_properties and DescriptionHolder do not match',16,1)
        ROLLBACK
        RETURN
    END

this code will rollback and abort the trigger if the DescriptionHolder is not 100% in sync with the actual column descriptions in the database. 如果DescriptionHolder与数据库中的实际列描述不是100%同步,则此代码将回滚并中止触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM