ConcurrencyMode unit tests for Entity Framework EDMX files

When using Microsoft’s Entity Framework, the ConcurrencyMode attribute for properties mapped to rowversion (a.k.a. timestamp) database columns, should be set to ConcurrencyMode.Fixed, to enable optimistic concurrency.

Unfortunately, due to a bug in the Entity Framework tools, you have to do this manually. The NuGet package EFConcurrencyModeTest makes it easy to write the unit tests needed to make sure that you have done so.

Easier done than said

Finding all columns with the incorrect ConcurrencyMode setting in an EDMX file is quite trivial. First, you make a set of all columns of type rowversion (or timestamp, depending on database version) in the SSDL part of an EDMX file. Then, you make a set of all columns where ConcurrencyMode = Fixed in the CSDL part. Subtract the latter set from the former, and you end up with a set containing all columns where ConcurrencyMode should have been set to Fixed but isn’t. I’ve put this logic in a class named ConcurrencyModeTester and this is how you use it from C# in NUnit:

using EFConcurrencyModeTest;
using NUnit.Framework;
using System.Reflection;

public class TestDbConcurrencyMode
    static void ConcurrencyModes()
        var cmt = new ConcurrencyModeTester();
        var asm = Assembly.GetAssembly(typeof(MyDatabaseEntitiesType));
        var result = cmt.BadConcurrencyModes(asm);
        Assert.IsEmpty(result, cmt.FormatBadConcurrencyModes(result));

The above example will check for bad ConcurrencyMode settings in all EDMX files embedded in the assembly loaded by the var asm = Assembly.GetAssembly(...); line.

The source code and documentation is hosted on GitHub and the package is available on NuGet.

The most trivial package on NuGet?

Why bother writing GitHub pages and documentation and building a NuGet package for such a trivial piece of code, barely larger than the good old Hello World example? Well, my reasons are purely self educational. In a blog post, Tomas Petricek describes how you can build, test, generate API docs, build NuGet packages and publish code, docs and packages in a fully automated process using FAKE (F# make). Now, who can resist that? I wanted to get hands on experience of this, and the result is EFConcurrencyModeTest.

In the process, I also learned that I vastly prefer Mercurial over Git, but that’s another story.

  1. No comments yet.

  1. No trackbacks yet.