在我的项目中,我有EF with ADO.NET
. 假设我有Entity Framework
以下课程
class Product
{
int Id { get; set;}
string Name { get; set; }
int TypeId { get; set; }
int CategoryId { get; set; }
}
class Type
{
int Id { get; set; }
string Name { get; set; }
}
class Category
{
int Id { get; set; }
string Name { get; set; }
}
然后我有navigation properties
:
- hasCategory:从 Category(Id) 到 Product(CategoryId),从 1 到 Many
- hasType:从 Type(Id) 到 Product(TypeId),从 1 到 Many
因此,如果我想访问产品的特定类别名称或类型名称(给定上下文):
int productId = 1;
var categoryName = context.Products.Single(p => p.Id == productId).hasCategory.Name;
var typeName = context.Products.Single(p => p.Id == productId).hasType.Name;
现在,如果我有属性名称,我可以获得该属性:
string propertyName = "CategoryId";
var propertyValue = GetType(Product).GetProperty(propertyName)
鉴于propertyName
我想知道是否有任何方法可以获取匹配的导航属性(hasCategory
或hasType
)以获取名称。Oterhwise 我不知道我应该在这两个类中Category
寻找Type
哪一个。