C# 如何在实体框架(EF6)中获得sql表的扩展属性?
我在VS-2013工作。我添加了C# 如何在实体框架(EF6)中获得sql表的扩展属性?,c#,sql-server-2008,entity-framework-6,t4,edmx,C#,Sql Server 2008,Entity Framework 6,T4,Edmx,我在VS-2013工作。我添加了ADO.NET实体数据模型,其中包含: 数据库中的EF设计器 首先从数据库中获取代码 连接到我的数据库中的一些表,它生成了带有分部类的tt文件,并在表的每个字段上添加注释,如下所示: ms\u项目\u类别cs 我已通过以下方式从数据库检索扩展属性: 通过Nuget Tiraggo库添加(安装程序包Tiraggo.Edmx)。它提供了对edmx文件中一些有用元数据的访问 将下面列出的引用和名称空间添加到*.tt文件中(出于您的目的,其中一些引用和名称空间可能是多余的
ADO.NET实体数据模型
,其中包含:
tt
文件,并在表的每个字段上添加注释,如下所示:
ms\u项目\u类别cs
我已通过以下方式从数据库检索扩展属性:
安装程序包Tiraggo.Edmx
)。它提供了对edmx文件中一些有用元数据的访问
和
foreach
循环内部创建一个info对象,以获取具体实体的元数据
var entityInfo=new TiraggoEntityInfo(edmx,entity.FullName)代码>
string extendedPropertyValue = GetExtendedProperty(dbContext, entityInfo.StorageInfo.Schema, entityInfo.StorageInfo.Name, edmProperty.Name, extendedPropName);
另外,希望这会有帮助。很抱歉格式化不正确,您的问题是想在对象中看到这些扩展属性吗?是的,我想在
model.tt
T4模板文件中显示SQL表的扩展属性。
select p.*, t.*
from sys.extended_properties p
inner join sys.tables t on p.major_id = t.object_id
where class = 1
<#@ assembly name="$(ProjectDir)TiraggoEdmx\TiraggoEdmx.EF.dll" #>
<#@ assembly name="System.Configuration" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Xml.Serialization" #>
<#@ import namespace="TiraggoEdmx_v3" #>
<#@ import namespace="System.Data.Entity" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Linq" #>
<#+ public static string GetExtendedProperty(DbContext context, string schema, string tableName, string columnName, string propName) {
var query = context.Database.SqlQuery<string>(string.Format("SELECT value FROM fn_listextendedproperty (NULL, 'schema', '{0}', 'table', '{1}', 'column', '{2}') where objname = '{2}' AND name = '{3}'", schema, tableName, columnName, propName));
return query.FirstOrDefault();
}#>
string extendedPropertyValue = GetExtendedProperty(dbContext, entityInfo.StorageInfo.Schema, entityInfo.StorageInfo.Name, edmProperty.Name, extendedPropName);