IoC 프레임 워크로 autofac 사용.
내 응용 프로그램을 시작할 때 내 DbContext
인스턴스를 설정할 수 있기를 원합니다.Entity Framework + Autofac - 저장시 임의 오류
내 ASP.NET MVC 3 프로젝트에서 DbContext
인스턴스를 Global.asax (PerLifetimeScope
)에 등록합니다. 그러나 한 번에 여러 브라우저 (또는 여러 탭)에서 내 사이트를 실행하면 가끔으로 변경 사항을 데이터베이스에 다시 저장하려고하면 Object reference not set to an instance of an object.
또는 New transaction is not allowed because there are other threads running in the session
이 표시됩니다. 또한 ExecuteReader requires an open and available Connection. The connection's current state: Broken.
때로는 데이터베이스에서 데이터를 읽고 싶습니다.
오류가 무작위로 팝업되는 것처럼 보입니다. 내 문맥의 수명 범위와 관련이 있다고 생각됩니다. 여기에 내 DbContext가 재정의 SaveChange
메서드입니다. 난 그냥 모든 작품을 좋아 브라우저에서 내 사이트의 하나 개의 탭이 열려있는 경우
builder.Register(c => new MyContext("SomeConnectionString"))
.InstancePerLifetimeScope();
: 여기
public class MyContext : DbContext
{
public override int SaveChanges()
{
var result = base.SaveChanges(); // Exception here
}
}
내가 내 컨텍스트를 등록하는 방법입니다.
또한 Ajax를 사용하여 컨트롤러 메소드를 호출하여 내 웹 사이트에서 5 ~ 10 초마다 db와 CRUD 작업을 수행 할 수 있습니다. New transaction is not allowed because there are other threads running in the session
에 대한
스택 트레이스 : Object reference not set to an instance of an object.
에 대한
at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.EntityClient.EntityConnection.BeginTransaction()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MyProject.Data.MyContext.SaveChanges() in D:\Test.cs
스택 트레이스는 :
at System.Data.Objects.ObjectStateManager.DetectConflicts(IList`1 entries)
at System.Data.Objects.ObjectStateManager.DetectChanges()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
at System.Data.Entity.Internal.InternalContext.GetStateEntries()
at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
at System.Data.Entity.DbContext.GetValidationErrors()
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MyProject.Data.MyContext.SaveChanges() in D:\Test.cs at
이 정보가 도움이 되나요? http://stackoverflow.com/questions/6237280/new-transaction-is-not-allowed-because-there-are-other-threads-running-in-the-se – Boomer
제 이해는'InstancePerLifetimeScope'가 컨텍스트 생성을 처리해야합니다./각 HttpRequest에 대해 별도로 처리해야합니다. 그렇지 않다면 내 컨텍스트에 대한 사용자 정의 평생 범위 관리자를 작성하는 참조가 있습니까? – Kamyar
나는이 문제도 잘 알고있다. –