title: Sql Server Compact Edition concurrency / thread safety (i.e. not thread safe)
body: <p>Over the last few months I have been developing an application using Entity Framework code first and sql server CE for the first time. I have found the combination of the 2 very useful, and compared to my old way of doing things (particularly ADO.NET) it allows for insanely faster dev times.</p>
  
  <p>However, this morning me and some colleagues came across a problem which we have never seen in any documentation regarding SqlServer CE. It cannot handle more than one insert at once!</p>
  
  <p>I was of the opinion that CE may become my database of choice until I came across this problem. The reason I discovered this was in my application I needed to make multiple requests to a web service at once, and it was introducing a bit of a bottleneck so I proceeded to use a <code>Parallel.Invoke</code> call to make the multiple requests.</p>
  
  <p>This was all working fine untill I turned on my applications message logging service. At this point I began to get the following error when making the web requests:</p>
  
  <pre><code>A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  
  Exception Details: System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]
  </code></pre>
  
  <p>Strange I thought. And my first recation was that it must be something to do with the <code>DbContext</code>, maybe the <code>DbContext</code> I was using was static or something else in my <code>Repository</code> class was static and causing the problem, but after sniffing around I was certaing it was nothing to do with my code.</p>
  
  <p>I then brought it to the attention of my colleagues and after a while it was decided it must be SqlServer CE, and after us all setting up different test projects attempting to recreate the problem using threads it was recreated almost every time, and when using Sql Server Express the problem wasn't ocurring.</p>
  
  <p>I just think it is a bit strange that CE cannot handle something as simple as this. I mean the problem is not only with threading - are you telling me that it cannot be used for a web application where two users may insert into a table at the same time...INSANITY!</p>
  
  <p>Anyway, just wondering if anyone else has come across this late into a project like me and been shocked (and annoyed) that it works this way? Also if anyone could shed light on why it is limited in this way that would be cool.</p>