【发布时间】:2016-08-09 08:14:00
【问题描述】:
我们知道我们不需要使用 Data Annotations 来配置一对多关系,按约定配置一对多关系。
在以下示例中,ICollection<Student> Students 是一个关系属性
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual Standard Standard { get; set; }
}
public class Standard
{
public Standard()
{
Students = new List<Student>();
}
public int StandardId { get; set; }
public string Description { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
所以我的问题是,如何检测给定类型的关系属性? 我的目标是测试属性的值以及它包含多少项
类似的东西:
static void Test(object givenInstanse)
{
foreach (PropertyInfo p in TryGetOneToManyRelationshipsPropertys(typeof(givenInstanse), dc))
{
var val = (ICollection)p.GetValue(givenInstanse);
Console.WriteLine(val.Count);
}
}
static IEnumerable<PropertyInfo> TryGetOneToManyRelationshipsPropertys(Type t, DbContext dc)
{
// ...
}
【问题讨论】:
标签: c# entity-framework ef-code-first code-first