在 C# 中从 LINQ 提取 SQL 列扩展属性

发布于 2024-08-06 21:37:49 字数 71 浏览 5 评论 0原文

我有一个 SQL 表,每列都有扩展属性。

有没有办法使用 Linq2SQL 从 C# 中的 LINQ 访问这些?

i have an SQL table with extended properties on each column.

Is there a way to access these from LINQ in c# using Linq2SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-08-13 21:37:49

像“MS_Description”之类的东西?不,据我所知;您可以编写一个与数据库中的存储进行对话的 SP(获取数据库对象名称和列名称),并通过数据上下文进行查询 - 但没有任何内置内容。

编写一些使用的代码也非常容易Expression 获取要传入的数据库名称(而不是 OO 名称)。类似于:(

    public static string GetProperty<TContext, TValue>(
        this TContext ctx, Expression<Func<TContext, TValue>> selector,
        string propertyName)
        where TContext : DataContext
    {
        MemberExpression me = selector.Body as MemberExpression;
        if (me == null) throw new InvalidOperationException();
        var member = me.Member;
        var objType = me.Expression.Type;
        var metaType = ctx.Mapping.GetMetaType(objType);
        string tableName = metaType.Table.TableName;
        string columnName = metaType.GetDataMember(member).MappedName;

        return ctx.GetProperty(tableName, columnName, propertyName);
    }

或类似的内容;只需启动测试数据库...)

在您提供 GetProperty 的位置 通过映射的 SPROC 方法。

更新:是的,这有点管用;示例:

string desc = ctx.GetProperty(x => x. DataChanges.First().Change, "MS_Description");

First() 很痛苦,但比必须有两个选择器要好一些;不过,它可以这样重写:

string desc = ctx.GetProperty(x => x.DataChanges, dc => dc.Change, "MS_Description");

由你决定,哪一个不那么hacky。另请注意,您需要将所有者从 tableName 中分离出来。

Things like "MS_Description" etc? Not AFAIK; you could write an SP that talks to the store at the database (taking the database object name and column name), and query that via the data-context - but nothing built in.

It would also be pretty easy to write some code that uses Expression to get the database names (instead of the OO names) to pass in. Something like:

    public static string GetProperty<TContext, TValue>(
        this TContext ctx, Expression<Func<TContext, TValue>> selector,
        string propertyName)
        where TContext : DataContext
    {
        MemberExpression me = selector.Body as MemberExpression;
        if (me == null) throw new InvalidOperationException();
        var member = me.Member;
        var objType = me.Expression.Type;
        var metaType = ctx.Mapping.GetMetaType(objType);
        string tableName = metaType.Table.TableName;
        string columnName = metaType.GetDataMember(member).MappedName;

        return ctx.GetProperty(tableName, columnName, propertyName);
    }

(or something similar; just firing up a test db...)

Where you provide the GetProperty method via a mapped SPROC.

Update: yes, that kinda works; example:

string desc = ctx.GetProperty(x => x. DataChanges.First().Change, "MS_Description");

The First() is a pain, but less ugly than having to have two selectors; it could be re-written that way, though:

string desc = ctx.GetProperty(x => x.DataChanges, dc => dc.Change, "MS_Description");

Up to you which is less hacky. Note also that you'd need to split the owner out of tableName.

疯到世界奔溃 2024-08-13 21:37:49

您想以什么方式访问它们?我的 VS 2008 L2S 插件可以将它们转换为 xml 文档注释(因此它们显示在 VS 智能感知中)和描述属性(您可以从代码访问)。请参阅“从数据库更新文档”功能,网址为 http://www.huagati.com/dbmltools/ ...

In what way do you want to access them? My L2S add-in for VS 2008 can turn them into xml doc comments (so they show up in VS intellisense), and description attributes [that you can access from code]. See the 'update documentation from database' feature at http://www.huagati.com/dbmltools/ ...

时光倒影 2024-08-13 21:37:49

您可以在数据库中创建一个视图,从 sys 视图中选择您需要的所有信息 - 从 sys.extended_properties 视图(在 SQL2005 及更高版本中)开始,并根据需要加入其他视图(在您的情况下,我认为是 sys.tables 和 sys.all_columns )。
然后只需将该视图导入 VS 中的模型中即可。

或者,您可以使用 SMO 访问所有 SQL Server 元数据,包括表、列等的扩展属性。不过我相信这将是 LINQ to Objects。
顺便说一下,这是查询此信息的唯一可移植(跨 SQL Server 版本)的方法。

http://msdn.microsoft.com/en-us/library/ms162557.aspx

You could create a view in your DB selecting all the info you need from sys views - starting with sys.extended_properties view (in SQL2005 and later) and joining others as needed (sys.tables and sys.all_columns in your case I'd think).
Then just import that view in VS in your model.

Alternatively you could use SMO to access all your SQL Server metadata, including extended properties on tables, columns etc. That will be LINQ to Objects though I believe.
By the way this is the only portable (across SQL Server versions) way to query this information.

http://msdn.microsoft.com/en-us/library/ms162557.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文