【问题标题】:How can I get the description of the table fields in my database using Entity Framework 6?如何使用 Entity Framework 6 获取数据库中表字段的描述?
【发布时间】:2017-11-07 14:42:46
【问题描述】:

如何获取:数据类型,长度,如果为空,表中列的主键

Users id primary key
email varchar(100) not null
active Boolean

【问题讨论】:

    标签: c# asp.net-mvc-4 entity-framework-6


    【解决方案1】:

    EF 对此不提供任何支持 - 您需要使用常规的原始 SQL 语句来查询 SQL Server 系统目录视图

    SELECT 
        TableName = t.Name,
        ColumnName = c.Name,
        c.is_nullable,
        TypeName = ty.name,
        c.max_length,
        PKName = i.name,
        PKColumn = pkc.name
    FROM 
        sys.tables t
    INNER JOIN 
        sys.columns c ON c.object_id = t.object_id
    INNER JOIN 
        sys.types ty ON ty.system_type_id = c.system_type_id
    LEFT OUTER JOIN 
        sys.indexes i ON i.object_id = t.object_id AND i.is_primary_key = 1
    LEFT OUTER JOIN 
        sys.index_columns ic ON ic.index_id = i.index_id AND ic.object_id = t.object_id
    LEFT OUTER JOIN 
        sys.columns pkc ON pkc.column_id = ic.column_id AND pkc.object_id = i.object_id
    

    【讨论】:

    • muchas gracias por su respuesta para ejecutar este tipo de comando,我必须使用吗? ctx.Database.ExecuteSqlCommand()
    • @iverGonzalezvidal: 要么,是的,要么使用带有SqlConnectionSqlCommand 的“经典” ADO.NET 连接来处理此问题
    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多