代码之家  ›  专栏  ›  技术社区  ›  profanis

存储过程失去连接

  •  0
  • profanis  · 技术社区  · 15 年前

    我有一个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
    }
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   mkchandler    15 年前

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

    这就是我执行实体框架命令和连接的方式(至少对于小型的简单项目):

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

    在实例化上下文时,您还可以选择传入一个连接字符串(如果没有,它将使用app.config中的连接字符串):

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

    对象上下文 . 您至少可以尝试这样做,看看它是否有效;这将告诉您这是否是连接字符串的问题。