title: ASP.NET MVC 5 SignalR, SqlDependency and EntityFramework 6 - the sqlparameter is already contained by another sqlparametercollection
body: <p>I am beginner with SignalR and SQLDepedency. I am trying to implement SignalR  using EF Code First Approach. I am getting the error <strong>The sqlparameter is already contained by another sqlparametercollection</strong> if I am using where condition in LINQ class.</p>
  
  <pre><code>public class MessageHub : Hub
  {
      internal NotifierEntity NotifierEntity { get; private set; }
      private MyDbContext db = new MyDbContext();
      public void DispatchToClient()
      {
          Clients.All.broadcastMessage("Refresh");
      }
  
  
      public void Initialize(String userName)
      {
          if (!string.IsNullOrEmpty(userName))
          {
              NotifierEntity = db.GetNotifierEntity&lt;Messages&gt;(db.Messages.Where(x=&gt;x.ApplicationUser.UserName== userName &amp;&amp; !x.Status));
              if (NotifierEntity == null)
                  return;
              Action&lt;String&gt; dispatcher = (t) =&gt; { DispatchToClient(); };
              PushSqlDependency.Instance(NotifierEntity, dispatcher);
          }
      }
  
  }
  </code></pre>
  
  <p>The NotifierEntity Class</p>
  
  <pre><code>public class NotifierEntity
  {
      ICollection&lt;SqlParameter&gt; sqlParameters = new List&lt;SqlParameter&gt;();
  
      public String SqlQuery { get; set; }
  
      public String SqlConnectionString { get; set; }
  
      public ICollection&lt;SqlParameter&gt; SqlParameters
      {
          get
          {
              return sqlParameters;
          }
          set
          {
              sqlParameters = value;
          }
      }
  
      public static NotifierEntity FromJson(String value)
      {
          if (String.IsNullOrEmpty(value))
              throw new ArgumentNullException("NotifierEntity Value can not be null!");
          return new JavaScriptSerializer().Deserialize&lt;NotifierEntity&gt;(value);
      }
  }
  
  public static class NotifierEntityExtentions
  {
      public static String ToJson(this NotifierEntity entity)
      {
          if (entity == null)
              throw new ArgumentNullException("NotifierEntity can not be null!");
          return new JavaScriptSerializer().Serialize(entity);
      }
  }
  
  public class PushSqlDependency
  {
      static PushSqlDependency instance = null;
      readonly SqlDependencyRegister sqlDependencyNotifier;
      readonly Action&lt;String&gt; dispatcher;
  
      public static PushSqlDependency Instance(NotifierEntity notifierEntity, Action&lt;String&gt; dispatcher)
      {
          if (instance == null)
              instance = new PushSqlDependency(notifierEntity, dispatcher);
          return instance;
      }
  
      private PushSqlDependency(NotifierEntity notifierEntity, Action&lt;String&gt; dispatcher)
      {
          this.dispatcher = dispatcher;
          sqlDependencyNotifier = new SqlDependencyRegister(notifierEntity);
          sqlDependencyNotifier.SqlNotification += OnSqlNotification;
      }
  
      internal void OnSqlNotification(object sender, SqlNotificationEventArgs e)
      {
          dispatcher("Refresh123");
      }
  }
  
  public class SqlDependencyRegister
  {
      public event SqlNotificationEventHandler SqlNotification;
  
      readonly NotifierEntity notificationEntity;
  
      internal SqlDependencyRegister(NotifierEntity notificationEntity)
      {
          this.notificationEntity = notificationEntity;
          RegisterForNotifications();
      }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security",
      "CA2100:Review SQL queries for security vulnerabilities")]
      void RegisterForNotifications()
      {
          using (var sqlConnection = new SqlConnection(notificationEntity.SqlConnectionString))
          using (var sqlCommand = new SqlCommand(notificationEntity.SqlQuery, sqlConnection))
          {
              foreach (var sqlParameter in notificationEntity.SqlParameters)
              {
                  sqlCommand.Parameters.Add(sqlParameter);
              }
  
              sqlCommand.Notification = null;
              var sqlDependency = new SqlDependency(sqlCommand);
              sqlDependency.OnChange += OnSqlDependencyChange;
              if (sqlConnection.State == ConnectionState.Closed)
                  sqlConnection.Open();
              sqlCommand.ExecuteNonQuery();
          }
      }
  
      void OnSqlDependencyChange(object sender, SqlNotificationEventArgs e)
      {
          if (SqlNotification != null)
              SqlNotification(sender, e);
          RegisterForNotifications();
      }
  }
  
  public delegate void SqlNotificationEventHandler(object sender, SqlNotificationEventArgs e);
  </code></pre>
  
  <p>If I am using the same query without any parameters, the code is working perfectly. I can see the database changes instantly in frontend. The issue is coming after added a parameter in Where clause.</p>
  
  <p>I got this idea from below link
  <a href="https://www.codeproject.com/Tips/1075852/ASP-NET-MVC-SignalR-SqlDependency-and-EntityFramew" rel="nofollow noreferrer">https://www.codeproject.com/Tips/1075852/ASP-NET-MVC-SignalR-SqlDependency-and-EntityFramew</a></p>
  
  <p>Sourcecode link</p>
  
  <p>we.tl/njwwLl8g36</p>