Effort 테스트 라이브러리를 사용하여 데이터베이스의 메모리 인스턴스를 만들고이 흥미로운 시나리오를 실행합니다.
DbContext1
열기 Table
항목 추가 (저장 안 함) DbContext1
닫기 DbContext2
열기 Table
Effort.Exceptions.EffortException : Database has not been initialized.
나는 카운트를 수행 할 경우,도 (5 단계) DbContext1
다음의 카운트 DbContext2
실패하지 않습니다?
전체 코드 :
public void TestEF()
{
var factory = new InMemoryMyApplicationDbContextFactory();
using (var db = factory.CreateDbContext())
{
//uncomment this line to prevent exception - why????
//db.Orders.Count();
db.Orders.Add(new Order{ Id = Guid.NewGuid() });
// intentionally do not call db.SaveChanges()
}
using (var db = factory.CreateDbContext())
{
// throws an exception if no read was performed above
db.Orders.Count();
}
}
전 예외 :
Effort.Exceptions.EffortException : Database has not been initialized.
If using CodeFirst try to add the following line:
context.Database.CreateIfNotExists()
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
예외 메시지에 언급 된 조언을 취하고 using
문 내에서 다음 줄을 테스트에 추가했습니다.
db.Database.CreateIfNotExists();
그리고 그것은 나를 위해 일했다.