作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Entity Framework 代码优先 DbContext,配置了以下实体。在这个例子中,Bar 类是 Foo 类的一个子类。
public class Foo
{
public Guid Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public Guid Id { get; set; }
public Guid FooId { get; set; }
public virtual Foo Foo { get; set; }
}
var context = new FooBarDbContext();
var bar = context.Set<Bar>().First();
// I want this method to return bar.FooId when passed the expression b => b.Foo
object result = MyService.GetForeignKeyValue(bar, b => b.Foo);
最佳答案
我实际上能够通过使用 GetDependentProperties 来确定外键属性。 NavigationProperty的方法类(class)。
这是我使用(或多或少)获得所需内容的代码:
object[] GetForeignKeyPropertyValues<TEntity, TRelatedEntity>(TEntity entity, Expression<Func<TEntity, TRelatedEntity>> navigationProperty)
{
if (entity == null)
return new object[] { };
// Find the entity metadata in the object context.
// (Assume you have access to the DbContext through the property CurrentDbContext.)
var objectContext = (CurrentDbContext as IObjectContextAdapter).ObjectContext;
var metadataNamespace = ObjectContextAdapter.GetType().Namespace;
var entityIdentity = metadataNamespace + "." + typeof(TEntity).Name; // HACK: This seems to work to retrieve the EntityType for an entity.
var entityMetadata = objectContext.MetadataWorkspace.GetItem<EntityType>(entityIdentity, DataSpace.CSpace);
// TODO: Verify that the entity metadata was found.
// Get the navigation property metadata by parsing the name from the navigation property expression.
var navigationPropertyName = GetPropertyName(navigationProperty);
var navigationPropertyMetadata = entityMetadata.NavigationProperties.FirstOrDefault(np => np.Name == navigationPropertyName);
// TODO: (JMB) Verify that the navigation property metadata was found.
// Extract the foreign key columns from the navigation property.
var foreignKeyPropertyMetadatas = navigationPropertyMetadata.GetDependentProperties();
// Create property getters for each foreign key property.
var foreignKeyPropertyGetters = foreignKeyPropertyMetadatas
.Select(propertyMetadata => MakePropertyGetter<TEntity>(propertyMetadata.Name))
.ToArray();
// Execute the foreign key property getters to get the foreign key property values for the specified entity.
var foreignKeyPropertyValues = foreignKeyPropertyGetters
.Select(propertyGetter => propertyGetter(entity))
.ToArray();
return foreignKeyPropertyValues;
}
static string GetPropertyName<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> navigationProperty)
{
var lambda = navigationProperty as LambdaExpression;
var member = lambda.Body as MemberExpression;
return member.Member.Name;
}
static Func<TEntity, object> MakePropertyGetter<TEntity>(Type entityType, string propertyName)
{
var parameterExpression = Expression.Parameter(typeof(TEntity), "entity");
var propertyExpression = Expression.PropertyOrField(parameterExpression, propertyName);
var lambdaExpression = Expression.Lambda(propertyExpression, parameterExpression);
var lambdaFunction = lambdaExpression.Compile();
return (Func<TEntity, object>)lambdaFunction;
}
关于entity-framework-4 - Entity Framework 代码优先 : How can I determine the foreign key property used for a navigation property at runtime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442801/
我是一名优秀的程序员,十分优秀!