在 C# 中从 LINQ 提取 SQL 列扩展属性
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像“MS_Description”之类的东西?不,据我所知;您可以编写一个与数据库中的存储进行对话的 SP(获取数据库对象名称和列名称),并通过数据上下文进行查询 - 但没有任何内置内容。
编写一些使用的代码也非常容易
Expression
获取要传入的数据库名称(而不是 OO 名称)。类似于:(或类似的内容;只需启动测试数据库...)
在您提供 GetProperty 的位置 通过映射的 SPROC 方法。
更新:是的,这有点管用;示例:
First()
很痛苦,但比必须有两个选择器要好一些;不过,它可以这样重写:由你决定,哪一个不那么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:(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:
The
First()
is a pain, but less ugly than having to have two selectors; it could be re-written that way, though:Up to you which is less hacky. Note also that you'd need to split the owner out of
tableName
.您想以什么方式访问它们?我的 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/ ...
您可以在数据库中创建一个视图,从 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