jeudi 3 septembre 2015

How to update entities in the DbContext without knowing their exact Type?

I have an

object elementToUpdate;

and i want to update it in my DbContext.

This seems to be a good starting point

public void Update<TEntity>(TEntity entity)
{
    DbContext.Set<TEntity>().Attach(entity);
    DbContext.Entry(entity).State = EntityState.Modified;
    DbContext.SaveChanges();
}

as explained here

My problem is, that i cannot cast my elementToUpdate properly. I know the name of it's precise type as string (e.g. "MyEntityType") but i don't know how to cast it using reflection - or in other words how to insert the Type-Parameter

DbContext.Set<MyEntityType>() 

from the string

"MyEntityType"

Thanks for your help.





Aucun commentaire:

Enregistrer un commentaire