2010-11-10 18 views
0

J'ai un projet ASP.NET MVC dans lequel le modèle est géré via des entités .NET et il semble que la connexion soit parfois perdue, mais cela ne se produit que sur des procédures stockées.La procédure stockée perd la connexion

je reçois l'erreur suivante:

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

Pourquoi est-ce qui se passe?

code

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


var catss = GetCategoriesStructure().ToList(); 

cette exception se produit quand je suis en train d'affecter la liste à CATSS variables

Contexte de l'instanciation d'objets

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

Dites-moi que vous n'essayez pas de garder une seule connexion ouverte pour toutes vos requêtes. – leppie

+0

J'ai oublié de mentionner le plus important. Peu importe que ce soit des formulaires mvc ou web, mais c'est important si le projet est développé avec des entités .net. Donc, je pense avoir répondu à votre question. Merci pour votre réponse. – StrouMfios

+0

Voulez-vous dire "géré par l'Entity Framework"? Veuillez fournir un exemple de code qui produit cette exception. –

Répondre

2

Sans savoir comment vous instanciation votre ObjectContext, je Je vais jeter quelque chose dans le seau de réponse ici.

Voilà comment je fais mes commandes Entity Framework et les connexions (pour les petits projets simples au moins):

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

Vous pouvez également passer en option dans une chaîne de connexion lors de l'instanciation votre contexte (sinon, il sera utiliser celui de votre app.config):

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

Si cela ne fonctionne pas votre problème, s'il vous plaît nous aider à comprendre votre code un peu mieux en affichant la façon dont vous créez votre ObjectContext. Vous pourriez au moins essayer de le faire de cette façon pour voir si cela fonctionne; cela vous dira si c'est un problème avec votre chaîne de connexion ou non.

+0

Thanks man! m'a beaucoup aidé! –