title: Error while adding entity object to DBContext object called from Parallel.Foreach loop
body: <p>Error : 
  If the event originated on another computer, the display information had to be saved with the event.</p>
  
  <p>The following information was included with the event: </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.    at
    System.Data.Objects.ObjectStateManager.DetectConflicts(IList<code>1
    entries)    at System.Data.Objects.ObjectStateManager.DetectChanges() 
    at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean
    force)    at
    System.Data.Entity.Internal.Linq.InternalSet</code>1.ActOnSet(Action action,
    EntityState newState, Object entity, String methodName)    at
    System.Data.Entity.Internal.Linq.InternalSet<code>1.Add(Object entity)<br>
    at System.Data.Entity.DbSet</code>1.Add(TEntity entity)    at
    ESHealthCheckService.BusinessFacade.BusinessOperationsLayer.AddErrorToDbObject(Exception
    ex, Server serverObj, Service windowsServiceObj)</p>
  </blockquote>
  
  <p>the message resource is present but the message is not found in the string/message table  </p>
  
  <pre><code>public void CheckForServerHealth()
          {
              businessLayerObj.SetStartTimeWindowsService();
              List&lt;ServerMonitor&gt; serverMonitorList = new List&lt;ServerMonitor&gt;();
              serverList = businessLayerObj.GetServerList();
              Parallel.ForEach(
            serverList,
            () =&gt; new List&lt;ServerMonitor&gt;(),
            (server, loop, localState) =&gt;
            {
                localState.Add(serverStatus(server, new ServerMonitor()));
                return localState;
            },
                localState =&gt;
                {
                    lock (serverMonitorList)
                    {
                        foreach (ServerMonitor serverMonitor in localState)
                        {
                            serverMonitorList.Add(serverMonitor);
                        }
                    }
                });
              businessLayerObj.SaveServerHealth(serverMonitorList);
          }
  
  
  public ServerMonitor serverStatus(Server serverObj, ServerMonitor serverMonitorObj)
          {
              if (new Ping().Send(serverObj.ServerName, 30).Status == IPStatus.Success)
              {
                  serverMonitorObj.Status = true;
                  try
                  {
                      PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total", serverObj.ServerName);
                      serverMonitorObj.CPUUtlilization = (cpu.NextValue());
                  }
                  catch (Exception ex)
                  {
                      businessLayerObj.AddErrorObjectToStaticList(ex, serverObj);
                  }
  
                  serverMonitorObj.ServerID = serverObj.ServerID;
                  try
                  {
                      string[] diskArray = serverObj.DriveMonitor.ToString().Split(':');
                      if (diskArray != null &amp;&amp; diskArray.Contains("NA"))
                      {
                          serverMonitorObj.DiskSpace = "NA";
                      }
                      else
                      {
                          serverMonitorObj.DiskSpace = ReadFreeSpaceOnNetworkDrives(serverObj.ServerName, diskArray);
                      }
                  }
                  catch (Exception ex)
                  {
                      businessLayerObj.AddErrorObjectToStaticList(ex, serverObj);
                  }
  
                  serverMonitorObj.CreatedDateTime = DateTime.Now;
              }
              else
              {
                  serverMonitorObj.Status = false;
                  serverMonitorObj.ServerID = serverObj.ServerID;
                  //return serverMonitorObj;
              }
  
              return serverMonitorObj;
          }
  
  
  
  public void AddErrorObjectToStaticList(Exception ex, Server serverObj = null, Service windowsServiceObj = null)
          {
              EShelathLoging esLogger = new EShelathLoging();
  
              esLogger.CreateDatetime = DateTime.Now;
              if (ex.InnerException != null)
              {
                  esLogger.Message = (windowsServiceObj == null ? ex.InnerException.Message : ("Service Name : " + windowsServiceObj.ServiceName + "--&gt;" + ex.InnerException.Message));
                  //esLogger.Message = "Service Name : " + windowsServiceObj.ServiceName + "--&gt;" + ex.InnerException.Message;
                  esLogger.StackTrace = (ex.InnerException.StackTrace == null ? "" : ex.InnerException.StackTrace);
              }
              else
              {
                  esLogger.Message = (windowsServiceObj == null ? ex.Message : ("Service Name : " + windowsServiceObj.ServiceName + "--&gt;" + ex.Message));
                  //esLogger.Message = "Service Name : " + windowsServiceObj.ServiceName + "--&gt;" + ex.Message;
                  esLogger.StackTrace = ex.StackTrace;
              }
  
              if (serverObj != null)
              {
                  esLogger.ServerName = serverObj.ServerName;
              }
              try
              {
                  lock (lockObject)
                  {
                      esHealthCheckLoggingList.Add(esLogger);
                  }
              }
              catch (Exception exe)
              {
                  string logEntry = "Application";
  
                  if (EventLog.SourceExists(logEntry) == false)
                  {
                      EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
                  }
  
                  EventLog eventLog = new EventLog();
                  eventLog.Source = logEntry;
                  eventLog.WriteEntry(exe.Message + " " + exe.StackTrace, EventLogEntryType.Error);
              }
          }
  </code></pre>
  
  <p>And then the below function is called to add objects from static list to the db object.</p>
  
  <p>public void AddErrorToDbObject()
          {
              try
              {
                  foreach (EShelathLoging eslogObject in esHealthCheckLoggingList)
                  {
                      lock (lockObject)
                      {
                          dbObject.EShelathLogings.Add(eslogObject);
                      }
                  }
              }
              catch (DbEntityValidationException exp)
              {
                  string logEntry = "Application";</p>
  
  <pre><code>            if (EventLog.SourceExists(logEntry) == false)
              {
                  EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
              }
  
              EventLog eventLog = new EventLog();
              eventLog.Source = logEntry;
              eventLog.WriteEntry(exp.Message + " " + exp.StackTrace, EventLogEntryType.Error);
          }
          catch (Exception exe)
          {
              string logEntry = "Application";
  
              if (EventLog.SourceExists(logEntry) == false)
              {
                  EventLog.CreateEventSource(logEntry, "Windows and IIS health check Log");
              }
  
              EventLog eventLog = new EventLog();
              eventLog.Source = logEntry;
              eventLog.WriteEntry(exe.Message + " " + exe.StackTrace, EventLogEntryType.Error);
          }`enter code here`
  
      }
  </code></pre>