Repositiory,OrderRequestRepository.cs統一的WebAPI的DbContext,但不能正常工作
public OrderRequestRepository(IntranetApplicationsContext context, ILogger logger)
{
_context = context;
_logger = logger;
}
...CRUD Methods
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
GC.SuppressFinalize(this);
}
Unity.WebApi UnityConfig.cs
container.RegisterType<IOrderRequestRepository, OrderRequestRepository>(new InjectionConstructor(new IntranetApplicationsContext() , new ElmahLogger()));
隨着UnityConfig.cs的上面的行首先調用api工程,第二次調用失敗,出現錯誤:
該操作無法完成因爲DbContext已經被處置。
如果我註釋掉_context.Dispose()線,那麼它的工作原理,這是很好的垃圾回收將清理對我來說,理想的情況我想管理它自己。
或者,如果我在UnityConfig.cs中使用下面的行代替 - 不使用InjectionConstructor,那麼它也可以正常工作。
container.RegisterType<ILogger, ElmahLogger>();
container.RegisterType<IOrderRequestRepository, OrderRequestRepository>();
但我想用InjectionConstructor因爲我想另一個參數添加到OrderRequestRepository構造和管理處置。關於爲什麼兩者都會導致錯誤的任何建議?
更新
我刪除了IDisposable代碼,由史蒂芬的建議,因爲
一般的規則是,誰創建對象的一個應該毀掉它,所以我會單獨留下我的EF dbcontext(IntranetApplicationsContext)。
IoC容器(Unity.Webapi在我的情況)應該處理處置
因此,與這條線在UnityConfig
container.RegisterType<IOrderRequestRepository, OrderRequestRepository> (new InjectionConstructor(new IntranetApplicationsContext(), new ElmahLogger()));
與EF讀取正在努力,但有趣的是,當我嘗試編輯我得到以下錯誤:
Attaching an entity of type 'IntranetApplications.Infrastructure.Models.OrderRequest' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
如果我回滾並使用b在UnityConfig asic線
container.RegisterType<IOrderRequestRepository, OrderRequestRepository>();
...一切正常。我相信這兩條線都使用了相同的默認生命期管理器TransientLifetimeManager,因此使用InjectionConstructor似乎仍然會破壞事情很奇怪。