I currently have the following working code:
private Converter RemoveAll<T>(List<T> databaseSet) {
databaseSet
.ForEach(item => this.myContext.Remove(item));
return this;
}
which I can invoke like:
this.RemoveAll(this.myContext.Widgets.ToList());
this.RemoveAll(this.myContext.WhositsWhatsits.ToList());
// etc.
Where this.myContext
is an instance of MyContext
which extends DbContext
; and Widgets
, WhositsWhatsits
, etc. are all instances of DbSet
.
Because I always need to change the DbSet
to a List
, I'd rather just pass the DbSet
as a parameter, like:
this.RemoveAll(this.myContext.Widgets);
this.RemoveAll(this.myContext.WhositsWhatsits);
// etc.
But if I change the method to:
private Converter RemoveAll<T>(DbSet<T> databaseSet) {
databaseSet.ToList()
.ForEach(item => this.myContext.Remove(item));
return this;
}
The compiler complains "The type 'T' must be a reference type in order to use it as a parameter 'TEntity' in the generic type or method 'DbSet'.
Is it possible to modify this method so I can remove the need to invoke ToList() before calling it?