Project Description
EntityFramework Generic DbContext, both generic base class and generic interface

    public class SampleContext : GuidDbContext
    {
        public IDbSet<Foo> Foos { get; set; }
        public IDbSet<Bar> Bars { get; set; }
    }

Or with multiple key types:

    public class SampleContext : GuidDbContext, IDbContext<int>
    {
        public IDbSet<Foo> Foos { get; set; }
        public IDbSet<Bar> Bars { get; set; }

        IQueryable<TEntity> IDbContext<int>.All<TEntity>(params Expression<Func<TEntity, object>>[] includeProperties)
        {
            return includeProperties.Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>(Set<TEntity>(), (current, includeProperty) => current.Include(includeProperty));
        }

        TEntity IDbContext<int>.Find<TEntity>(int id)
        {
            return Set<TEntity>().Find(id);
        }

        TEntity IDbContext<int>.Create<TEntity>()
        {
            return Set<TEntity>().Create();
        }

        int IDbContext<int>.Insert<TEntity>(TEntity entity)
        {
            Set<TEntity>().Add(entity);
            return entity.Id;
        }

        void IDbContext<int>.Update<TEntity>(TEntity entity)
        {
            Entry(entity).State = EntityState.Modified;
        }

        void IDbContext<int>.Delete<TEntity>(int id)
        {
            Set<TEntity>().Remove(Set<TEntity>().Find(id));
        }
    }

Last edited Mar 30, 2012 at 5:38 PM by ScottXu, version 4