Skip to content

Instantly share code, notes, and snippets.

@spewu
Forked from couellet/EntityFrameworkSession
Last active December 16, 2015 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spewu/5502965 to your computer and use it in GitHub Desktop.
Save spewu/5502965 to your computer and use it in GitHub Desktop.
public abstract class Entity
{
public int Id { get; set; }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
public class EntityFrameworkSession : ISession
{
private readonly DbContext context;
public EntityFrameworkSession(DbContext context)
{
this.context = context;
}
public T Find<T>(params int[] keyValues) where T : Entity
{
return context.Set<T>().Find(keyValues);
}
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : Entity
{
return context.Set<T>().SingleOrDefault(predicate);
}
public T FirstOrDefault<T>(Func<T, bool> predicate) where T : Entity
{
return context.Set<T>().FirstOrDefault(predicate);
}
public IQueryable<T> All<T>() where T : Entity
{
return context.Set<T>().AsQueryable();
}
public void Add<T>(T entity) where T : Entity
{
context.Set<T>().Add(entity);
}
public void AddOrUpdate<T>(params T[] entities) where T : Entity
{
context.Set<T>().AddOrUpdate(entities);
}
public void AddOrUpdate<T>(Expression<Func<T, object>> identifierExpression, params T[] entities) where T : Entity
{
context.Set<T>().AddOrUpdate(identifierExpression, entities);
}
public void LoadReference<T, TProperty>(T entity, Expression<Func<T, TProperty>> navigationProperty)
where T : Entity
where TProperty : class
{
context.Entry(entity).Reference(navigationProperty).Load();
}
public void LoadCollection<T, TElement>(T entity, Expression<Func<T, ICollection<TElement>>> navigationProperty)
where T : Entity
where TElement : class
{
context.Entry(entity).Collection(navigationProperty).Load();
}
public int Commit()
{
return context.SaveChanges();
}
public void Delete<T>(T entity) where T : Entity
{
context.Set<T>().Remove(entity);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public interface ISession
{
T Find<T>(params int[] keyValues) where T : Entity;
T SingleOrDefault<T>(Func<T, bool> predicate) where T : Entity;
T FirstOrDefault<T>(Func<T, bool> predicate) where T : Entity;
IQueryable<T> All<T>() where T : Entity;
void Add<T>(T entity) where T : Entity;
void AddOrUpdate<T>(params T[] entities) where T : Entity;
void AddOrUpdate<T>(Expression<Func<T, object>> identifierExpression, params T[] entities) where T : Entity;
void LoadReference<T, TProperty>(T entity, Expression<Func<T, TProperty>> navigationProperty)
where T : Entity
where TProperty : class;
void LoadCollection<T, TElement>(T entity, Expression<Func<T, ICollection<TElement>>> navigationProperty)
where T : Entity
where TElement : class;
int Commit();
void Delete<T>(T entity) where T : Entity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment