我正在尝试扩展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
}
}
答案 0 :(得分:0)
你应该尝试如下.Net 3.5
namespace Microsoft.Samples.Entity
{
/// <summary>
/// There are no comments for SalesOrdersEntities in the schema.
/// </summary>
public partial class SalesOrdersEntities : global::System.Data.Objects.ObjectContext
{
/// <summary>
/// Initializes a new SalesOrderEntities object using the connection string found in the 'SalesOrderEntities' section of the application configuration file.
/// </summary>
public SalesOrdersEntities() :
base("name=SalesOrdersEntities", "SalesOrdersEntities")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new SalesOrderEntities object.
/// </summary>
public SalesOrdersEntities(string connectionString) :
base(connectionString, "SalesOrdersEntities")
{
this.OnContextCreated();
}
/// <summary>
/// Initialize a new SalesOrderEntities object.
/// </summary>
public SalesOrdersEntities(global::System.Data.EntityClient.EntityConnection connection) :
base(connection, "SalesOrdersEntities")
{
this.OnContextCreated();
}
partial void OnContextCreated();
/// <summary>
/// There are no comments for LineItemSet in the schema.
/// </summary>
public global::System.Data.Objects.ObjectQuery<LineItem> LineItemSet
{
get
{
if ((this._LineItemSet == null))
{
this._LineItemSet = base.CreateQuery<LineItem>("[LineItemSet]");
}
return this._LineItemSet;
}
}
private global::System.Data.Objects.ObjectQuery<LineItem> _LineItemSet;
/// <summary>
/// There are no comments for OrderSet in the schema.
/// </summary>
public global::System.Data.Objects.ObjectQuery<Order> OrderSet
{
get
{
if ((this._OrderSet == null))
{
this._OrderSet = base.CreateQuery<Order>("[OrderSet]");
}
return this._OrderSet;
}
}
private global::System.Data.Objects.ObjectQuery<Order> _OrderSet;
/// <summary>
/// There are no comments for LineItemSet in the schema.
/// </summary>
public void AddToLineItemSet(LineItem lineItem)
{
base.AddObject("LineItemSet", lineItem);
}
/// <summary>
/// There are no comments for OrderSet in the schema.
/// </summary>
public void AddToOrderSet(Order order)
{
base.AddObject("OrderSet", order);
}
}
}