I would like to create a base class that is somewhat generic for all of my entities. The class would have methods like Save(), Delete(), GetByID() and some other basic functionality and properties. I have more experience with Linq to SQL and was hoping to get some good examples for something similar in the EF. Thank you.
Asked
Active
Viewed 4,424 times
2 Answers
2
Like this:
public abstract class BaseObject<T>
{
public void Delete(T entity)
{
db.DeleteObject(entity);
db.SaveChanges();
}
public void Update(T entity)
{
db.AcceptAllChanges();
db.SaveChanges();
}
}
public interface IBaseRepository<T>
{
void Add(T entity);
T GetById(int id);
IQueryable<T> GetAll();
}

Hector Minaya
- 1,695
- 3
- 25
- 45
-
What do you do with the interface? – wayne.blackmon Sep 18 '12 at 18:11
-
The class appears to be missing the DBContext object, "db" declaration. – wayne.blackmon Sep 18 '12 at 18:44
1
The ADO.NET Entity Framework supports both Table-per-hierarchy and Table-per-type inheritance. I suggest you start here to see how it works.

Scott Whitlock
- 13,739
- 7
- 65
- 114