I have not worked with Entity Framework or generics before and am having some difficulty reducing my code.
I am parsing a text file to load 10 lookup tables with data th
I have a generic repository in my current project. This is its add method:
public void Add<T>(T newItem) where T : class
{
db.Set<T>().Add(newItem);
}
where db
is the DbContext
object itself. The where T : class
fixes that error about reference types. Without it, you could pass any type in as T, including bool
or struct
, or any value type, which DbSet.Add()
can't handle. The where
specifies that T must be a class, which is a reference type, and therefore allowed.