MVC#ExecuteReader需要一个开放且可用的连接。连接';的当前状态是打开的。错误
我已将此添加到我的MVC RouteConfig中:MVC#ExecuteReader需要一个开放且可用的连接。连接';的当前状态是打开的。错误,c#,asp.net-mvc,entity-framework,asp.net-mvc-routing,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc Routing,我已将此添加到我的MVC RouteConfig中: routes.MapRoute( name: "categories", url: "{sub}", defaults: new { controller = "cat", action = "Index" } , constraints: new { sub = new MyCatConstraint() } ); 这是我的MyCatConstraint类: public class MyCatConstraint : IRouteCo
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需要开放且可用的连接。连接的当前状态为打开。
你能告诉我该怎么解决这个问题吗 Wrap
KhanoumiDbContext db=new KhanoumiDbContext()
在中使用
语句。您应该缓存tbl\u类别的内容
,这样您就不需要为每个选中的路由调用数据库。将其缓存在哪里?怎么用?另外,它应该是实时的,类别可以随时更改。问题的可能重复出现在另一个控制器中,我在其中使用了和,我的问题就解决了。