错误一:

程序完整报错:

A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.

中文翻译:

在上一个操作完成之前,在此上下文上启动了第二个操作。这通常是由使用同一DbContext实例的不同线程造成的,但是实例成员不能保证线程安全。这也可能是由于在客户机上计算的嵌套查询造成的,如果是这种情况,请重写查询以避免嵌套调用。

出现这个问题的原因是,我的数据库注入用的是单例模式,然后我在一个异步方法里用Task.Factory.StartNew开启了一个新线程,然后在这个新线程里操作了数据库。之后就发现这个错了。

解决办法:

1、避免在新开启的线程里操作数据库,可以单独封装方法然后通过返回值的方式去做一些处理。

2、重新New一个上下文

var optionsBuilder = new DbContextOptionsBuilder<SqlContext>();
optionsBuilder.UseSqlite(Configuration.GetConnectionString("Sqlite"));
using (var context = new SqlContext(optionsBuilder.Options))
{
//操作数据库的时候用context去保存
}

错误二:

完整错误:

Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'SqlContext'.

中文翻译:

无法访问已释放的对象。此错误的一个常见原因是释放从依赖项注入解析的上下文,然后稍后尝试在应用程序的其他位置使用相同的上下文实例。如果对上下文调用Dispose()或在using语句中包装上下文,则可能会发生这种情况。如果使用依赖项注入,则应让依赖项注入容器负责处理上下文实例。

对象名:“SqlContext”。

出现这个问题的原因是,发现第一个错误之后,我想那我不注入单例注入成别的类型不就好了,然后下面的错误就诞生了。其实还是和我在一个异步方法里用Task.Factory.StartNew开启了一个新线程,然后在这个新线程里操作了数据库有关。

解决办法:

既然说上下文释放了那我再New一个好了,和上面的解决办法一样。

var optionsBuilder = new DbContextOptionsBuilder<SqlContext>();
optionsBuilder.UseSqlite(Configuration.GetConnectionString("Sqlite"));
using (var context = new SqlContext(optionsBuilder.Options))
{
//操作数据库的时候用context去保存
}

这两种错误的发生,其实最终的解决方案就是不要在新开的线程里操作数据库,尽量避免就行了,就不用特殊处理了。

最新文章

  1. php 上传文件
  2. MySQL乱码问题
  3. js 实现动态key value(JSON字符串注意事项:key和value都要用双引号,官网指定用双引号)
  4. 开发环境中biztalk项目设置注意事项(转)
  5. C++面向对象要点
  6. java android 中的Toast
  7. scrollLeft,scrollWidth,clientWidth,offsetWidth 可实现导航栏固定不动(冻结)的效果
  8. mycat实例(3)
  9. mod_python模块安装
  10. C++利用不完全实例化来获得函数模板参数的返回值和参数
  11. Akka(3): Actor监管 - 细述BackoffSupervisor
  12. BZOJ2004: [Hnoi2010]Bus 公交线路
  13. 在Vue.js2.0中组件模板子元素数量问题
  14. 【Unity Shaders】Using Textures for Effects——打包和混合textures
  15. python 基础语法学习
  16. ELK学习笔记之kibana关闭和进程查找
  17. mybatis关于ORM的使用以及设计(二)[DaoInterface 转换 Mapper代理对象]
  18. 利用js给datalist或select动态添加option选项
  19. ios the request was denied by service delegate for reason unspecified
  20. C# semphore的使用

热门文章

  1. Tomcat 部署多个web项目
  2. java对接微信小程序
  3. learning java AWT 布局管理器CardLayout
  4. qt截图grapWindow,操作系统剪切版QClipBoard实现进程间通信
  5. leetcode解题报告(24):Pascal&#39;s TriangleII
  6. C++多重继承分析——《虚继承实现原理(虚继承和虚函数)》
  7. Educational Codeforces Round 64(ECR64)
  8. avalon中的ms-attr?
  9. LeetCode 第 154 场周赛
  10. Assignment4:闰年判断输入异常时的处理方法