访问导航属性的协会详细信息 [英] Access Association Details of NavigationProperty

查看:139
本文介绍了访问导航属性的协会详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于.edmx文件的C#T4模板来构建一些类,到目前为止这么好。我现在需要的是通过



2种实现方式:

  //获取对您感兴趣的导航属性的引用
var navProp = GetNavigationProperty();
//加载元数据工作空间
MetadataWorkspace metadataWorkspace = null;
bool allMetadataLoaded = loader.TryLoadAllMetadata(inputFile,out metadataWorkspace);

//从存储模型获取关联类型
var association = metadataWorkspace
.GetItems< AssociationType>(DataSpace.SSpace)
.Single(a => ; a.Name == navProp.RelationshipType.Name)

//然后查看引用约束
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(ForeignKeys中的NavigationProperty foreignKey)
{
foreach(GetSourceSchemaTypes< AssociationType>()中的var rc
.Single(x => x.Name = = foreignKey.RelationshipType.Name)
.ReferenceConstraints)
{
foreach(rc.ToProperties中的var tp)
WriteLine(tp.Name);
foreach(var fp in rc.FromProperties)
WriteLine(fp.Name);
}
}


I'm creating a C# T4 template to scaffold some classes based on .edmx file, so far so good. What I now need though, is a way to access the name of the columns it connects in the database, through a NavigationProperty.

I realized, not long ago, that you can access this information in the .edmx visual designer, under mapping details for an specific NavigationProperty:

So basically, if in the T4 template; I already have an instance of the NavigationProperty I want... How can I get the names of the fields it connects? (WeatherOnMondays in this case)

解决方案

Answers from: EF4: Get the linked column names from NavigationProperty of an EDMX

2 ways of achieving this:

// 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));

2nd approach:

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);
   }
}

这篇关于访问导航属性的协会详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆