jeudi 17 décembre 2015

How to iterate through an ICollection type property with reflection

The title covers a very tiny part of what I'm trying to achieve, so be informed ahead. I'm trying to build a generic way of properly updating collection properties of an entity when that entity itself is updated. In the nutshell I want to do something similar to the approach explained here but I want to go generic way. For that I have created an attribute called EntityCollectionPropertyAttribute and marked those properties of entities that I need to be updated too. Here's an example:

   public class Teacher{
      public int TeacherId{get;set;}
      public string Name{get;set;}
   }

   public class Student{
      public int StudentId{get;set;}
      public string Name {get;set;}
      [EntityCollectionProperty]
      public virtual ICollection<Teacher> Teachers{get;set;}
   }


 public bool IsPropertyAnEntityCollection(PropertyInfo prop){
     return Attribute.IsDefined(prop, typeof(EntityCollectionPropertyAttribute));
 }

 public void Update<T>(T entity)where T:class{
    DbEntityEntry entry = MyDbContext.Entry(entity);
    foreach (var prop in entry.Entity.GetType().GetProperties())
    {
        if(IsPropertyAnEntityCollection(prop)){
            //Here's where I get stuck
        }
    }
 }

Lets say the parent entity that has been updated is a Student. Besides Name (and possibly ID) properties, I need the Teachers to be updated as well. So in the commented area I need something like this:

 var updatedTeachers=studentEntity.Teachers.ToList();

but of course generic way. I will also have to look inside the DbContext for the teachers DBSet independently. So I will need something like this too:

var exisitingTeachers=MyDbContext.Teachers.ToList();

Any ideas how to do this?





Aucun commentaire:

Enregistrer un commentaire