我在我的 MVC 应用程序中找不到正确的方法来绑定存储库和所有 EF 相关数据。下面的示例为每个 IRepository 创建新的 DbContext,这给了我错误
一个实体对象不能被多个 IEntityChangeTracker 实例引用
出现此错误是因为我的实体在不同的上下文中。例如代码(它在实体中)将给出错误
var user = new User();
_userRepository.Insert(user)
var order = new Order();
order.User = user;
_orderRepository.Insert(order)
_unitOfWork.Commit();
如果我改变
kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString));
到
kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString)).InRequestScope();
我收到错误
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
如果我在单独的线程中使用 ServiceRepository。
也许有人知道解决方案?
var connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString));
kernel.Bind<IObjectSetFactory>().ToMethod(c => kernel.Get<DbContextAdapter>());
kernel.Bind<IObjectContext>().ToMethod(c => kernel.Get<DbContextAdapter>());
kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork));
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));
kernel.Bind<IServiceRepository>().To<ServiceRepository>();
public interface IServiceRepository
{
UserDetail GetUser(int id);
User GetUser(string email);
User GetUser(string email, string password);
OrderDetail GetOrder(string id);
IEnumerable<OrderDetail> GetOrders(int userId);
IEnumerable<Product> GetProducts();
UserDetail GetParentUser(string partialEmail);
IEnumerable<UserDetail> GetChildUsers(int parentId);
IEnumerable<Statistic> GetStatisticForCurrentMonth(string ip);
void InsertStatistic(QueueItem queueItem);
void InsertStatistic();
void Commit();
void AddUser(User model);
User AddUser(string firstName, string lastName, string email, string password, string country, int? parentId = null, DateTime? dateStamp = null);
void AddOrder(Order order);
void DeleteUser(int id);
void DeleteUser(string email);
bool OrderManager(PaymentProcessorOrder order, out User newUser, out Order newOrder);
User AuthenticatedUser();
string AuthenticatedUserEmail();
bool ValidateUser(string email, string password);
string GetPassword(string email);
}
public class ServiceRepository : IServiceRepository
{
private readonly IRepository<User> _userRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IRepository<UserDetail> _userDetailRepository;
private readonly IRepository<Statistic> _statisticRepository;
private readonly IRepository<Product> _productRepository;
private readonly IRepository<OrderDetail> _orderDetailRepository;
private readonly IUnitOfWork _unitOfWork;
private static readonly object Locker = new object();
public ServiceRepository(IRepository<User> userRepository, IRepository<Statistic> statisticRepository, IRepository<UserDetail> userDetailRepository, IRepository<Order> orderRepository, IUnitOfWork unitOfWork, IRepository<OrderDetail> orderDetailRepository, IRepository<Product> productRepository)
{
_unitOfWork = unitOfWork;
_userRepository = userRepository;
_statisticRepository = statisticRepository;
_userDetailRepository = userDetailRepository;
_orderRepository = orderRepository;
_orderDetailRepository = orderDetailRepository;
_productRepository = productRepository;
}
//Skip code
}