我正在尝试扩展 System.Data.Objects 3.5 版本以包含一些 System.Data.Objects .Net 4.0 方法(例如 IObjectSet、CreateObjectSet、ContextOptions)。我被迫使用 EF v 1 在 Sharepoint 2010 项目中以 3.5 为目标。我的存储库是在 .Net 4.0 中构建的,如下所示。如何更正此问题以在 3.5 中工作,或扩展 System.Data.Objects 3.5 类以便使用新的 4.0 方法:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using System.Linq.Expressions;
using System.Data;
using System.Data.Objects.DataClasses;
using Workflowportal_LINQDAL.Repository.Interface;
using Workflowportal_LINQDAL.Repository.Specification;
namespace Workflowportal_LINQDAL.Repository.Repository
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private ObjectContext _context;
private readonly IObjectSet<TEntity> _objectSet;
public Repository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<TEntity>();
//_context.ContextOptions.LazyLoadingEnabled = true; Default!
}
public Repository(ObjectContext context, bool lazyLoading)
{
_context = context;
_objectSet = _context.CreateObjectSet<TEntity>();
_context.ContextOptions.LazyLoadingEnabled = lazyLoading;
}
public void SaveChanges()
{
_context.SaveChanges();
}
//public void SaveChanges(SaveOptions options)
//{
// _context.SaveChanges(options);
//}
public void AcceptAllChanges()
{
_context.AcceptAllChanges();
}
public void Add(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_objectSet.AddObject(entity);
}
public void Edit(TEntity entity)
{
_objectSet.Attach(entity);
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
public void Delete(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_objectSet.DeleteObject(entity);
}
public void Delete(Expression<Func<TEntity, bool>> predicate)
{
var records = Find(predicate);
foreach (var record in records)
{
Delete(record);
}
}
public void Delete(ISpecification<TEntity> specification)
{
throw new NotImplementedException();
}
public void DeleteRelatedEntities(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
var releatedEntities =
((IEntityWithRelationships)entity).RelationshipManager.GetAllRelatedEnds().SelectMany(
e => e.CreateSourceQuery().OfType<TEntity>()).ToList();
foreach (var releatedEntity in releatedEntities)
{
_objectSet.DeleteObject(releatedEntity);
}
_objectSet.DeleteObject(entity);
}
public IEnumerable<TEntity> GetAll()
{
return _objectSet.AsEnumerable();
}
public IQueryable<TEntity> GetAllQuery()
{
return _objectSet.AsQueryable();
}
public IEnumerable<TEntity> GetAllPaged(int page, int pageSize)
{
return _objectSet.AsEnumerable().Skip(pageSize).Take(page);
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return _objectSet.Where(predicate).AsEnumerable();
}
public IQueryable<TEntity> FindQuery(Expression<Func<TEntity, bool>> predicate)
{
return _objectSet.Where(predicate).AsQueryable();
}
public IEnumerable<TEntity> Find(ISpecification<Func<TEntity, bool>> specification)
{
throw new NotImplementedException();
}
public IEnumerable<TEntity> FindPaged(int page, int pageSize, Expression<Func<TEntity, bool>> predicate)
{
return _objectSet.Where(predicate).Skip(pageSize).Take(page).AsEnumerable();
}
public TEntity Single(Expression<Func<TEntity, bool>> predicate)
{
return _objectSet.SingleOrDefault(predicate);
}
public TEntity Single(ISpecification<Func<TEntity, bool>> specification)
{
throw new NotImplementedException();
}
public TEntity First(Expression<Func<TEntity, bool>> predicate)
{
return _objectSet.FirstOrDefault(predicate);
}
public TEntity First(ISpecification<Func<TEntity, bool>> specification)
{
throw new NotImplementedException();
}
public int Count()
{
return _objectSet.Count();
}
public int Count(Expression<Func<TEntity, bool>> criteria)
{
return _objectSet.Count(criteria);
}
public int Count(ISpecification<TEntity> specification)
{
throw new NotImplementedException();
}
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
if (_context == null) return;
_context.Dispose();
_context = null;
}
#endregion
}
}