Effort.Exceptions.EffortException : Database has not been initialized - after non commited update?

c# effort entity-framework entity-framework-6


Why does this exception occur? Is this a bug?

I'm using Effort, the EF testing library to create an in memory instance of my database and run into this interesting scenario:

  1. Open DbContext1
  2. Add item to Table (do not save)
  3. Close DbContext1
  4. Open DbContext2
  5. Count items in Table

Effort.Exceptions.EffortException : Database has not been initialized.

However, if I perform a count (step 5) also in DbContext1 then the Count in DbContext2 doesn't fail?

Full Code:

public void TestEF()
   var factory = new InMemoryMyApplicationDbContextFactory();

   using (var db = factory.CreateDbContext())
      //uncomment this line to prevent exception - why????

      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

Full Exception:

Effort.Exceptions.EffortException : Database has not been initialized.

If using CodeFirst try to add the following line:
 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)        
9/9/2017 11:20:44 PM

Popular Answer

I took the advice mentioned in the exception message and added the following line to my test within the using statement


and that worked for me.

4/14/2018 2:31:04 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow