Creating a base class for Entities in the Entity Framework

I would like to create a base class that is somewhat common to all of my objects. The class will have methods like Save (), Delete (), GetByID () and some other basic functions and properties. I have more experience with Linq to SQL and was hoping to get good examples for something like this in EF. Thanks.

+2


a source to share


2 answers


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();
    }

      

+2


a source


ADO.NET Entity Framework supports both hierarchical inheritance and per-type inheritance. I suggest you start here to find out how it works.



+1


a source







All Articles