I have a problem when adding posts to my DB, this "Add" function is called from multiple threads. And usually very quick after eachother, I dont know if this matters, but thats the way it works.
Code:
private object dbLock = new object();
public void Add(string fileName, DateTime collectionTime)
{
try
{
lock (dbLock)
{
var collection = new Collection
{
Filename = fileName,
Datetime = collectionTime,
};
_entities.AddToCollection(collection);
_entities.SaveChanges();
CollectionChanged(collection, null);
}
}
catch (Exception ex)
{
}
finally
{
}
}
My problem is that when SaveChanges is called I get this exception:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
All answers I managed to find has something to do with that you have to add StoreGeneratedPattern="Identity", I allready did this, but it doesn't help
<Property Name="ID" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" />
So is there any other way to solve this?