2010-11-10 55 views
0

我有一个ASP.NET MVC项目,其中模型通过.NET实体进行管理,似乎有时会失去连接,但这只发生在存储过程上。存储过程失去连接

我得到以下错误:

Execution of the command requires an open and available connection. The connection's current state is broken. 

这究竟是为什么?

代码

public ObjectResult<Categories> GetCategoriesStructure() { 
     return ObjectContext.getCategoriesStructure(); 
    } 


var catss = GetCategoriesStructure().ToList(); 

当我试图分配清单catss出现此异常变量

对象上下文实例化

public abstract class ObjectContextManager { 
    /// <summary> 
    /// Returns a reference to an ObjectContext instance. 
    /// </summary> 
    public abstract TObjectContext GetObjectContext<TObjectContext>() 
     where TObjectContext : ObjectContext, new(); 
} 

public abstract class BaseDAO<TObjectContext, TEntity> : IBaseDAO<TObjectContext, TEntity> 
    where TObjectContext : System.Data.Objects.ObjectContext, new() 
    where TEntity : System.Data.Objects.DataClasses.EntityObject { 

    private ObjectContextManager _objectContextManager; 

    /// <summary> 
    /// Returns the current ObjectContextManager instance. Encapsulated the 
    /// _objectContextManager field to show it as an association on the class diagram. 
    /// </summary> 
    private ObjectContextManager ObjectContextManager { 
     get { return _objectContextManager; } 
     set { _objectContextManager = value; } 
    } 

    /// <summary> 
    /// Returns an ObjectContext object. 
    /// </summary> 
    protected internal TObjectContext ObjectContext { 
     get { 
      if (ObjectContextManager == null) 
       this.InstantiateObjectContextManager(); 

      return ObjectContextManager.GetObjectContext<TObjectContext>(); 
     } 
    } 

    /// <summary> 
    /// Default constructor. 
    /// </summary> 
    public BaseDAO() { } 

    /// <summary> 
    /// Instantiates a new ObjectContextManager based on application configuration settings. 
    /// </summary> 
    private void InstantiateObjectContextManager() { 
     /* Retrieve ObjectContextManager configuration settings: */ 
     Hashtable ocManagerConfiguration = ConfigurationManager.GetSection("ObjectContextManagement.ObjectContext") as Hashtable; 
     if (ocManagerConfiguration != null && ocManagerConfiguration.ContainsKey("managerType")) { 
      string managerTypeName = ocManagerConfiguration["managerType"] as string; 
      if (string.IsNullOrEmpty(managerTypeName)) 
       throw new ConfigurationErrorsException("The managerType attribute is empty."); 
      else 
       managerTypeName = managerTypeName.Trim().ToLower(); 

      try { 
       /* Try to create a type based on it's name: */ 
       Assembly frameworkAssembly = Assembly.GetAssembly(typeof(ObjectContextManager)); 
       Type managerType = frameworkAssembly.GetType(managerTypeName, true, true); 

       /* Try to create a new instance of the specified ObjectContextManager type: */ 
       this.ObjectContextManager = Activator.CreateInstance(managerType) as ObjectContextManager; 
      } catch (Exception e) { 
       throw new ConfigurationErrorsException("The managerType specified in the configuration is not valid.", e); 
      } 
     } else 
      throw new ConfigurationErrorsException("ObjectContext tag or its managerType attribute is missing in the configuration."); 
    } 

    /// <summary> 
    /// Persists all changes to the underlying datastore. 
    /// </summary> 
    public void SaveAllObjectChanges() { 
     this.ObjectContext.SaveChanges(); 
    } 

    /// <summary> 
    /// Adds a new entity object to the context. 
    /// </summary> 
    /// <param name="newObject">A new object.</param> 
    public virtual void Add(TEntity newObject) { 
     this.ObjectContext.AddObject(newObject.GetType().Name, newObject); 
    } 
    /// <summary> 
    /// Deletes an entity object. 
    /// </summary> 
    /// <param name="obsoleteObject">An obsolete object.</param> 
    public virtual void Delete(TEntity obsoleteObject) { 
     this.ObjectContext.DeleteObject(obsoleteObject); 
    } 

    public void Detach(TEntity obsoleteObject) { 
     this.ObjectContext.Detach(obsoleteObject); 
    } 

    /// <summary> 
    /// Updates the changed entity object to the context. 
    /// </summary> 
    /// <param name="newObject">A new object.</param> 
    public virtual void Update(TEntity newObject) { 
     ObjectContext.ApplyPropertyChanges(newObject.GetType().Name, newObject); 
     ObjectContext.Refresh(RefreshMode.ClientWins, newObject); 
    } 

    public virtual TEntity LoadByKey(String propertyName, Object keyValue) { 
     IEnumerable<KeyValuePair<string, object>> entityKeyValues = 
      new KeyValuePair<string, object>[] { 
     new KeyValuePair<string, object>(propertyName, keyValue) }; 

     // Create the key for a specific SalesOrderHeader object. 
     EntityKey key = new EntityKey(this.ObjectContext.GetType().Name + "." + typeof(TEntity).Name, entityKeyValues); 
     return (TEntity)this.ObjectContext.GetObjectByKey(key); 
    } 

    #region IBaseDAO<TObjectContext,TEntity> Members 


    public bool validation(TEntity newObject) { 
     return newObject.GetType().Name.ToString() == "Int32"; 
    } 

    #endregion 
} 
+9

请告诉我你并没有试图保持一个连接打开所有的查询。 – leppie 2010-11-10 10:59:30

+0

我忘了提及最重要的。无论它是mvc还是web表单都无所谓,但如果项目是使用.net实体开发的,那么这很重要。所以,我想我已经回答了你的问题。感谢您通过方式回复 – StrouMfios 2010-11-10 18:11:11

+0

您的意思是“通过实体框架进行管理”?请提供产生该异常的示例代码。 – 2010-11-10 18:54:54

回答

2

不知道你是如何实例化你的ObjectContext,我我会在这里的答案桶里扔东西。

这是我做我的实体框架的命令和连接(至少简单的小项目):

using (MyEntities context = new MyEntities()) 
{ 
    return context.getCategoriesStructure(); 
} 

您也可以选择通过一个连接字符串中实例化的上下文时(如果没有,它会使用您的app.config一):

new MyEntities("...connection string...") 

如果这没有帮助您的问题,请帮助我们理解你的代码有点张贴你如何创建ObjectContext更好。你至少可以尝试这样做,看看它是否有效;它会告诉你这是否是连接字符串的问题。

+0

谢谢!帮了我很多! – 2010-11-24 22:32:45