zoukankan      html  css  js  c++  java
  • System.InvalidOperationException: Unexpected connection state. When using a wrapping provider ensure that the StateChange event is implemented on the wrapped DbConnection.

    System.InvalidOperationException: Unexpected connection state. When using a wrapping provider ensure that the StateChange event is implemented on the wrapped DbConnection.

    System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.TimeoutException: Timeout in IO operation

    开发windows 服务 使用EF6遇到的坑!

    1、有时候调试停个一两分钟,然后继续调试可能就会出现这个错误,然后无法连接数据库。

    2、其它未知错误也可能会引发该错误。

    解决办法:创建一个新的DbContext即可解决

    /** 

          //Aotufac依赖注入 配置接口依赖
           builder.RegisterType<MyModel>().As<DbContext>();//每次调用生成一个dbcontext对象

           //注册将当前程序集的类
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
              .AsImplementedInterfaces().AsSelf();
           _container = builder.Build(); 

           /// <summary>
            /// 从Autofac容器获取对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static T GetFromFac<T>()
            {
                //DependencyResolver.Current.GetService<ClassType>()
                return _container.Resolve<T>();
            }

    **/

    var mydbcontext;//构造函数注入

    try{

       //你执行的代码。。。。。

       mydbcontext.xxxx();

    }catch(System.InvalidOperationException inverr)

    {

        mydbcontext =GetFromFac<MyModel>();//重新获取一个dbcontext对象,如果不是用的autofac  重新new 一个dbcontext对象

        //Creating a new db-context solved the problem.

       //创建一个新的DbContext即可解决

    }

     如果是打开一个web网页报这样的错,重新打开一个页面,但是windows服务不可能每次都去重启,只能重新创建连接

  • 相关阅读:
    easyui-tree/combotree 子节点前端懒加载(主要解决ie11下加载慢
    解决 Chrome 下表单自动填充问题 (两种方法
    代码编辑器:本地JS文件上传并加载到页面
    PC端使用rem进行屏幕适配
    ECharts 点击非图表区域的点击事件不触发问题
    Angular2+ 使用 Post 请求下载文件
    Express + Element-ui 实现图片/文件上传
    phpMyAdmin -- 没有权限操作用户
    Note of Moment -- 日期处理
    Angular 自定义表单控件 -- CheckboxGroupComponent
  • 原文地址:https://www.cnblogs.com/pzxnet/p/13094340.html
Copyright © 2011-2022 走看看