我正在使用Effort,EF测试库来创建我的数据库的内存实例并遇到这个有趣的场景:
@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...
}
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();
}
}
答案 0 :(得分:1)
我接受了异常消息中提到的建议,并在using
语句中将以下行添加到我的测试中
db.Database.CreateIfNotExists();
这对我有用。