Контекст не зависит от потока, то есть один и тот же.
public class BaseDBProxy
{
protected SiteContext db = new SiteContext();
public TEntity GetById<TEntity>(int id)
{
return db.Set<TEntity>.Where(e => e.Id == id).FirstOrDefault();
}
}
public abstract class RepositoryBase<TEntity> where TEntity : class
{
private Set<TEntity> _set;
public RepositoryBase(DbContext context)
{
_set = context.Set<TEntity>();
}
public TEntity GetById(int id)
{
return _set.Where(e => e.Id == id).First();
}
public TEntity TryGetById(int id)
{
return _set.Where(e => e.Id == id).FirstOrDefault();
}
}
public class ExchangerProvider
{
public static IEnumerable<Robofollower.Exchanger> LikesList(int userId)
{
using (var context = new DataClassesDataContext())
{
return context.Exchanger
.Where(e => e.UserId != userId)
.Where(e => e.LikeActive)
.Where(e => e.Points >= 1)
.Where
(e =>
context.LikeExchanger_Data
.Where(l => l.UserId == userId)
.All(l => l.MediaId != e.MediaId)
)
.OrderByDescending(e => e.Points)
.Take(5)
.ToList();
}
}
}