2

我正在创建一个 C# T4 模板来构建一些基于 .edmx 文件的类,到目前为止一切都很好。不过,我现在需要的是一种通过NavigationProperty访问它在数据库中连接的列的名称的方法。

不久前,我意识到您可以在 .edmx 视觉设计器中访问此信息,在特定 NavigationProperty 的映射详细信息下: 在此处输入图像描述

所以基本上,如果在 T4 模板中;我已经有一个我想要的 NavigationProperty 实例...如何获取它连接的字段的名称?(WeatherOnMondays在这种情况下)

4

4 回答 4

3

来自的答案:EF4:从 EDMX 的 NavigationProperty 获取链接的列名

实现此目的的 2 种方法:

// Obtain a reference to the navigation property you are interested in
var navProp = GetNavigationProperty();
// Load the metadata workspace
MetadataWorkspace metadataWorkspace = null;
bool allMetadataLoaded =loader.TryLoadAllMetadata(inputFile, out metadataWorkspace);

// Get the association type from the storage model
var association = metadataWorkspace
    .GetItems<AssociationType>(DataSpace.SSpace)
    .Single(a => a.Name == navProp.RelationshipType.Name)

// Then look at the referential constraints
var toColumns = String.Join(",", 
    association.ReferentialConstraints.SelectMany(rc => rc.ToProperties));
var fromColumns = String.Join(",", 
    association.ReferentialConstraints.SelectMany(rc => rc.FromProperties));

第二种方法:

NavigationProperty[] foreignKeys = entity.NavigationProperties
  .Where(np => np.DeclaringType == entity &&
          ((AssociationType)np.RelationshipType).IsForeignKey).ToArray();

foreach (NavigationProperty foreignKey in foreignKeys)
{
   foreach(var rc in GetSourceSchemaTypes<AssociationType>()
       .Single(x => x.Name == foreignKey.RelationshipType.Name)
       .ReferentialConstraints)
   {
       foreach(var tp in rc.ToProperties)
           WriteLine(tp.Name);
       foreach(var fp in rc.FromProperties)
           WriteLine(fp.Name);
   }
}
于 2012-07-30T23:29:11.843 回答
0

如果您有NavigationProperty,它连接的字段由属性ToEndMemberFromEndMember

于 2012-07-30T08:03:48.343 回答
0

这段代码更简单。它在我的 Visual Studio 2012 上运行良好。AssociationType 类的详细信息可以在 http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.associationtype.aspxhttp://msdn找到.microsoft.com/en-us/library/system.data.metadata.edm.referentialconstraint.aspx

<#@ template language="C#" debug="true" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#
string inputFile = @"DomainModel.edmx";

MetadataLoader loader = new MetadataLoader(this);

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
    foreach (NavigationProperty navProperty in entity.NavigationProperties)
    {
        AssociationType association = ItemCollection.GetItems<AssociationType>().Single(a => a.Name == navProperty.RelationshipType.Name);
        string fromEntity = association.ReferentialConstraints[0].FromRole.Name;
        string fromEntityField = association.ReferentialConstraints[0].FromProperties[0].Name;
        string toEntity = association.ReferentialConstraints[0].ToRole.Name;
        string toEntityField = association.ReferentialConstraints[0].ToProperties[0].Name;
    }
}

#>
于 2013-04-09T03:34:22.020 回答
0

请参阅http://brewdawg.github.io/Tiraggo.Edmx/,您可以从 NuGet 安装它。它提供了 edmx 文件中的所有元数据,包括所有映射、每列的低级 SQL 数据类型,所有这些东西,看看页面上的示例,你会发现它是多么容易。

于 2013-09-23T01:27:07.197 回答