C# EF5中实现的多线程计数器主键冲突

C# EF5中实现的多线程计数器主键冲突,c#,ef-code-first,C#,Ef Code First,我试图首先使用EF5代码实现一个一致的多读写器计数器,我遇到了并发异常(这是我预料到的),但我也遇到了主键约束冲突,这是我预料不到的。只有当计数器由代码创建时才会发生这种情况;如果已经存在,则按预期进行计数 下面是我正在使用的代码(以及调试代码): 我只是简单地使用了Parallel.For来调用它: EFCounter counter1 = new EFCounter(); EFCounter counter2 = new EFCounter(); Guid counterId = Guid

我试图首先使用EF5代码实现一个一致的多读写器计数器,我遇到了并发异常(这是我预料到的),但我也遇到了主键约束冲突,这是我预料不到的。只有当计数器由代码创建时才会发生这种情况;如果已经存在,则按预期进行计数

下面是我正在使用的代码(以及调试代码):

我只是简单地使用了
Parallel.For
来调用它:

EFCounter counter1 = new EFCounter();
EFCounter counter2 = new EFCounter();

Guid counterId = Guid.NewGuid();
Parallel.For(
    1,
    10,
    i =>
        {
            Console.WriteLine("1: {0}", counter1.CreateOrIncrement(counterId));
            Console.WriteLine("2: {0}", counter2.CreateOrIncrement(counterId));
        }
    );
作为参考,连接字符串:

<add name="MsSqlViewModel" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=ESRaffleViewModels;Integrated Security=SSPI" />

这是在我的机器上运行的代表性输出;我从未真正完成过出现UpdateExceptions的跑步:

1: 1000001 2: 1000002 UpdateExceptions: 1 1: 1000003 UpdateExceptions: 2 2: 1000004 UpdateExceptions: 3 1: 1000005 UpdateExceptions: 4 2: 1000006 UpdateExceptions: 5 UpdateExceptions: 6 UpdateExceptions: 7 UpdateExceptions: 8 UpdateExceptions: 9 UpdateExceptions: 10 UpdateExceptions: 11 UpdateExceptions: 12 UpdateExceptions: 13 UpdateExceptions: 14 UpdateExceptions: 15 UpdateExceptions: 16 UpdateExceptions: 17 UpdateExceptions: 18 UpdateExceptions: 19 UpdateExceptions: 20 UpdateExceptions: 21 UpdateExceptions: 22 UpdateExceptions: 23 UpdateExceptions: 24 UpdateExceptions: 25 UpdateExceptions: 26 UpdateExceptions: 27 UpdateExceptions: 28 UpdateExceptions: 29 UpdateExceptions: 30 UpdateExceptions: 31 UpdateExceptions: 32 UpdateExceptions: 33 UpdateExceptions: 34 UpdateExceptions: 35 UpdateExceptions: 36 UpdateExceptions: 37 UpdateExceptions: 38 UpdateExceptions: 39 UpdateExceptions: 40 UpdateExceptions: 41 UpdateExceptions: 42 UpdateExceptions: 43 UpdateExceptions: 44 UpdateExceptions: 45 UpdateExceptions: 46 UpdateExceptions: 47 UpdateExceptions: 48 UpdateExceptions: 49 UpdateExceptions: 50 UpdateExceptions: 51 UpdateExceptions: 52 UpdateExceptions: 53 UpdateExceptions: 54 UpdateExceptions: 55 UpdateExceptions: 56 UpdateExceptions: 57 UpdateExceptions: 58 UpdateExceptions: 59 UpdateExceptions: 60 UpdateExceptions: 61 UpdateExceptions: 62 UpdateExceptions: 63 UpdateExceptions: 64 UpdateExceptions: 65 UpdateExceptions: 66 UpdateExceptions: 67 UpdateExceptions: 68 UpdateExceptions: 69 UpdateExceptions: 70 UpdateExceptions: 71 UpdateExceptions: 72 UpdateExceptions: 73 UpdateExceptions: 74 UpdateExceptions: 75 UpdateExceptions: 76 UpdateExceptions: 77 UpdateExceptions: 78 UpdateExceptions: 79 UpdateExceptions: 80 UpdateExceptions: 81 UpdateExceptions: 82 UpdateExceptions: 83 UpdateExceptions: 84 UpdateExceptions: 85 UpdateExceptions: 86 UpdateExceptions: 87 UpdateExceptions: 88 UpdateExceptions: 89 UpdateExceptions: 90 UpdateExceptions: 91 UpdateExceptions: 92 UpdateExceptions: 93 UpdateExceptions: 94 UpdateExceptions: 95 UpdateExceptions: 96 UpdateExceptions: 97 UpdateExceptions: 98 UpdateExceptions: 99 UpdateExceptions: 100 1: 1000001 2: 1000002 更新异常:1 1: 1000003 更新异常:2 2: 1000004 更新异常:3 1: 1000005 更新异常:4 2: 1000006 更新异常:5 更新异常:6 更新异常:7 更新异常:8 更新异常:9 更新例外:10 更新例外:11 更新例外:12 更新例外:13 更新例外:14 更新例外:15 更新例外:16 更新例外:17 更新例外:18 更新例外:19 更新例外:20 更新例外:21 更新例外:22 更新例外:23 更新例外:24 更新例外:25 更新例外:26 更新例外:27 更新例外:28 更新例外:29 更新例外:30 更新例外:31 更新异常:32 更新例外:33 更新例外:34 更新例外:35 更新例外:36 更新例外:37 更新例外:38 更新例外:39 更新例外:40 更新例外:41 更新例外:42 更新例外:43 更新例外:44 更新例外:45 更新例外:46 更新例外:47 更新例外:48 更新例外:49 更新例外:50 更新例外:51 更新例外:52 更新例外:53 更新例外:54 更新例外:55 更新例外:56 更新例外:57 更新例外:58 更新例外:59 更新例外:60 更新例外:61 更新例外:62 更新例外:63 更新异常:64 更新例外:65 更新例外:66 更新例外:67 更新例外:68 更新例外:69 更新例外:70 更新例外:71 更新例外:72 更新例外:73 更新例外:74 更新例外:75 更新例外:76 更新例外:77 更新例外:78 更新例外:79 更新异常:80 更新例外:81 更新例外:82 更新例外:83 更新例外:84 更新例外:85 更新例外:86 更新例外:87 更新例外:88 更新例外:89 更新例外:90 更新例外:91 更新例外:92 更新例外:93 更新例外:94 更新例外:95 更新例外:96 更新例外:97 更新例外:98 更新例外:99 更新例外:100 我在这里遗漏了一些基本的东西吗?

锁定(此)并不是EFCounter的两个实例所独有的,因此这基本上是无用的。您需要一个静态对象


您需要使用数据库来处理数据库并发性

锁定
这个
不应该是这两个实例所独有的-我将其设计为分布式服务的概念证明,并且碰巧选择了EF进行尝试。你介意指出一些你在对这个问题的实际评论中看到的“更脏”的东西吗?如果锁不是独占的,它是无用的。如果你没有阻止两个线程同时做同一件事,为什么要锁呢?正如我所说的,我把它设计成一个分布式服务。例如,当作为单例运行时,锁用于帮助并发性。我提供的测试用例旨在模拟2个服务实例运行并同时更新同一个表。粗体显示“分布式”不会使锁(这)在上面的代码中执行任何操作。数据库用于序列化数据库事务。我们使用像asp.net这样的并行多线程框架,因为并发性总是一个问题。这是一个关于为什么不应该在不需要的时候使用并行操作的练习。我需要处理并行不是这里的问题。我只是好奇为什么我的实现没有像我期望的那样运行。我在下面给出了答案。锁定两个线程都可以看到的东西。将“实现”斜体化并不能改变这样一个事实,即你的实验是徒劳的。您正在从数据库中删除并发处理,并错误地实现它。当线程在不同的机器上运行时,我应该如何锁定两个线程都可以看到的东西?我如何通过在代码中使用
lock
语句从数据库中移除并发处理?如果使用单独的进程,则将数据库用作序列化(锁定/解锁)机制。通过不使用数据库进行并发,您将从数据库中移除并发处理。 1: 1000001 2: 1000002 UpdateExceptions: 1 1: 1000003 UpdateExceptions: 2 2: 1000004 UpdateExceptions: 3 1: 1000005 UpdateExceptions: 4 2: 1000006 UpdateExceptions: 5 UpdateExceptions: 6 UpdateExceptions: 7 UpdateExceptions: 8 UpdateExceptions: 9 UpdateExceptions: 10 UpdateExceptions: 11 UpdateExceptions: 12 UpdateExceptions: 13 UpdateExceptions: 14 UpdateExceptions: 15 UpdateExceptions: 16 UpdateExceptions: 17 UpdateExceptions: 18 UpdateExceptions: 19 UpdateExceptions: 20 UpdateExceptions: 21 UpdateExceptions: 22 UpdateExceptions: 23 UpdateExceptions: 24 UpdateExceptions: 25 UpdateExceptions: 26 UpdateExceptions: 27 UpdateExceptions: 28 UpdateExceptions: 29 UpdateExceptions: 30 UpdateExceptions: 31 UpdateExceptions: 32 UpdateExceptions: 33 UpdateExceptions: 34 UpdateExceptions: 35 UpdateExceptions: 36 UpdateExceptions: 37 UpdateExceptions: 38 UpdateExceptions: 39 UpdateExceptions: 40 UpdateExceptions: 41 UpdateExceptions: 42 UpdateExceptions: 43 UpdateExceptions: 44 UpdateExceptions: 45 UpdateExceptions: 46 UpdateExceptions: 47 UpdateExceptions: 48 UpdateExceptions: 49 UpdateExceptions: 50 UpdateExceptions: 51 UpdateExceptions: 52 UpdateExceptions: 53 UpdateExceptions: 54 UpdateExceptions: 55 UpdateExceptions: 56 UpdateExceptions: 57 UpdateExceptions: 58 UpdateExceptions: 59 UpdateExceptions: 60 UpdateExceptions: 61 UpdateExceptions: 62 UpdateExceptions: 63 UpdateExceptions: 64 UpdateExceptions: 65 UpdateExceptions: 66 UpdateExceptions: 67 UpdateExceptions: 68 UpdateExceptions: 69 UpdateExceptions: 70 UpdateExceptions: 71 UpdateExceptions: 72 UpdateExceptions: 73 UpdateExceptions: 74 UpdateExceptions: 75 UpdateExceptions: 76 UpdateExceptions: 77 UpdateExceptions: 78 UpdateExceptions: 79 UpdateExceptions: 80 UpdateExceptions: 81 UpdateExceptions: 82 UpdateExceptions: 83 UpdateExceptions: 84 UpdateExceptions: 85 UpdateExceptions: 86 UpdateExceptions: 87 UpdateExceptions: 88 UpdateExceptions: 89 UpdateExceptions: 90 UpdateExceptions: 91 UpdateExceptions: 92 UpdateExceptions: 93 UpdateExceptions: 94 UpdateExceptions: 95 UpdateExceptions: 96 UpdateExceptions: 97 UpdateExceptions: 98 UpdateExceptions: 99 UpdateExceptions: 100