【问题标题】:MVC C# ExecuteReader requires an open and available Connection. The connection's current state is open. errorMVC C# ExecuteReader 需要一个打开且可用的连接。连接的当前状态是打开的。错误
【发布时间】:2016-01-10 06:07:35
【问题描述】:

我已将此添加到我的 MVC RouteConfig:

routes.MapRoute(
name: "categories",
url: "{sub}",
defaults: new { controller = "cat", action = "Index" }
, constraints: new { sub = new MyCatConstraint() }
);

这是我的 MyCatConstraint 类:

 public class MyCatConstraint : IRouteConstraint
    {



        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
             KhanoumiDbContext db = new KhanoumiDbContext();



            if (values.ContainsKey(parameterName))
            {
                string thisCat = values["sub"].ToString();

                    return db.tbl_Category.Any(c => c.Cat_Name_En == thisCat);


            }
            return false;
        }

    }

添加这些后有时我会收到此错误:

ExecuteReader 需要一个打开且可用的连接。连接的当前状态为打开。

你能告诉我我应该怎么做才能解决这个问题吗?

【问题讨论】:

标签: c# asp.net-mvc entity-framework asp.net-mvc-routing


【解决方案1】:

KhanoumiDbContext db = new KhanoumiDbContext() 包装在using 语句中。

【讨论】:

  • 问题出在另一个控制器中,我在其中使用了using,我的问题解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多