Can I set SQL Server column descriptions using a free nhibernate card?

I use my current nhibernate mappings to create my MS SQL Server database.

I would like to be able to set column descriptions as part of this generation.

+3
source share
3 answers

No, It is Immpossible. The description is an attribute of Microsoft metadata, and both NHibernate and Fluent NHibernate try to aggregate the database as soon as possible.

You may be able to do this with a match SqlInsert, but it will not be very nice.

+4
source

, API, Fluent NHibernate NHibernate.

, , NHibernate , SQL-, .

, "" Fluent NHibernate, , , , .
( , .)

+1

, "", /. SchemaName, TableName, ColumnName Description. TRIGGER , SQL Server sp_addextendedproperty (Transact-SQL) sp_dropextendedproperty (Transact-SQL) / / schema.table.column.

:

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

:

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 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 roll back and cancel the launch if the DescriptionHolder is not 100% synchronized with the actual column descriptions in the database.

+1
source

Source: https://habr.com/ru/post/1745397/


All Articles