We know that we do not need to configure for one-to-many relationships using Data Annotations, one-to-many relationship configured by convention.
In following example ICollection<Student> Students
is an relationship property
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; }
}
So my question is, how can I detect a relationship propertys for a given type? My goal is to test the value of the property and how many items it contains
Something like that:
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)
{
// ...
}