Effort.Exceptions.EffortException:数据库尚未初始化 - 非提交更新后?

时间:2017-09-09 23:20:44

标签: c# entity-framework entity-framework-6 effort

为什么会发生此异常?这是一个错误吗?

我正在使用Effort,EF测试库来创建我的数据库的内存实例并遇到这个有趣的场景:

  1. 打开@ServerEndpoint(value = "/websocket/myendpoint") public class MyEndpoint{ @OnOpen public void onOpen(Session session, EndpointConfig config){ // by catching the exception and handling yourself // here, the @OnError will never be called. try{ Long token = HTTP.getRequiredLongParameter(session, "token"); // process your token } catch(BadRequestException e){ // as you suggested: session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "some text")); } } // @OnClose, @OnMessage, @OnError... }
  2. 将项目添加到DbContext1(不保存)
  3. 关闭Table
  4. 打开DbContext1
  5. 计算DbContext2
  6. 中的项目

    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();
       }    
    }    
    

1 个答案:

答案 0 :(得分:1)

我接受了异常消息中提到的建议,并在using语句中将以下行添加到我的测试中

    db.Database.CreateIfNotExists();

这对我有用。