Im stuck on a generics issue. I have a method that returns DbSet from a DbContext object based on the object type.
Now I want to write I method where I pass in a object that implements a interface called IBaseData (that all entities implements). I want to use the existing method to return the correct DbSet based on the type of the IBaseData object, but I can't get it to work.
I've tried various versions of , typeof and gettype.
Existing method:
public GenericRepository<T> GetRepo<T>() where T : class
{
return new GenericRepository<T>(_context);
}
New method (this is just what Im trying to do):
public async Task SetDateLastInvoked<T>(IBaseData data, int id)
{
var tmp = GetRepo<typeof(data)>().Where(obj => obj.Id.Equals(id));
tmp.DateLastInvoked = DateTime.Now;
await SaveChangesAsync();
}
datais not known at compile time, to invoke the generic method you will need reflection as described here stackoverflow.com/questions/232535/…dataobject when it entersSetDateLastInvoked? More specifically, is it attached to a context ar that point?