如何在EF中引发DbUpdateConcurrencyException?

时间:2018-03-14 15:24:19

标签: c# entity-framework dbcontext

我想激发DbUpdateConcurrencyException以对其进行负面单元测试。

最简单的方法是什么?

我试过这样,但它没有错误地通过:

ClientRepository clientRepo = new ClientRepository(this._context, MockingHelper.GetIDataAccessLayerInformation());
try
{
    var client = clientRepo.DBSet.Single(x => x.ClientCode == 1);
    var clientSecondTime = clientRepo.DBSet.Single(x => x.ClientCode == 1);

    client.ClientDisplayName = "NewName";
    this._context.SaveChanges();

    clientSecondTime.ClientDisplayName = "NewNameForException";
    this._context.SaveChanges();
}
catch (DbUpdateConcurrencyException cex)
{
    Assert.Pass();
}

Assert.Fail();

0 个答案:

没有答案