EFConcurrencyModeTest


EFConcurrencyModeTest

EFConcurrencyModeTest helps you write unit tests (for e.g. NUnit) that test whether optimistic concurrency for Entity Framework is enabled in your EDMX files.

When using 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. EFConcurrencyModeTest helps you write the tests needed to make sure that you have done so.

EFConcurrencyModeTest can be installed from NuGet:
PM> Install-Package EFConcurrencyModeTest

F# Example

This example shows how a simple NUnit test using EFConcurrencyModeTest looks in F#:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
open EFConcurrencyModeTest
open NUnit.Framework
open System.Reflection

type TestDbConcurrencyMode () =
    [<Test>]
    static member ConcurrencyModes () =
        let cmt = ConcurrencyModeTester()
        let asm = Assembly.GetAssembly(typeof<MyDatabaseEntitiesType>)
        let result = cmt.BadConcurrencyModes(asm, "MyEdmxFileNameWithoutExtension")
        Assert.IsEmpty(result, cmt.FormatBadConcurrencyModes result)

C# Example

This example shows the same NUnit test in C#:

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

public class TestDbConcurrencyMode
{
    [Test]
    static void ConcurrencyModes()
    {
        var cmt = new ConcurrencyModeTester();
        var asm = Assembly.GetAssembly(typeof(MyDatabaseEntitiesType));
        var result = cmt.BadConcurrencyModes(asm, "MyEdmxFileNameWithoutExtension");
        Assert.IsEmpty(result, cmt.FormatBadConcurrencyModes(result));
    }
}

Documentation

The above examples show just about all you have to know about the usage of EFConcurrencyModeTest. There are a few additional variants of the BadConcurrencyModes function which are documented in the API Reference.

Fixing faulty concurrency modes

Bad concurrency modes can be fixed automatically by the FixEFConcurrencyModes utility. See this blog post for usage examples.

Copyright

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

namespace EFConcurrencyModeTest
namespace NUnit
namespace NUnit.Framework
namespace System
namespace System.Reflection
Multiple items
type TestDbConcurrencyMode =
  new : unit -> TestDbConcurrencyMode
  static member ConcurrencyModes : unit -> unit

Full name: Index.TestDbConcurrencyMode

--------------------
new : unit -> TestDbConcurrencyMode
Multiple items
type TestAttribute =
  inherit Attribute
  new : unit -> TestAttribute
  member Description : string with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
TestAttribute() : unit
static member TestDbConcurrencyMode.ConcurrencyModes : unit -> unit

Full name: Index.TestDbConcurrencyMode.ConcurrencyModes
val cmt : ConcurrencyModeTester
Multiple items
type ConcurrencyModeTester =
  new : unit -> ConcurrencyModeTester
  member BadConcurrencyModes : edmxFilePath:string -> EntityProperty []
  member BadConcurrencyModes : assembly:Assembly -> EntityProperty []
  member BadConcurrencyModes : assembly:Assembly * edmxName:string -> EntityProperty []
  member BadConcurrencyModes : csdlDocument:XDocument * ssdlDocument:XDocument * mslDocument:XDocument -> EntityProperty []
  member BadConcurrencyModes : csdlElement:XElement * ssdlElement:XElement * mslElement:XElement -> EntityProperty []
  member FormatBadConcurrencyModes : entityProperties:EntityProperty [] -> string
  member private GetXDocument : assembly:Assembly * resourceName:string -> XDocument
  member ConcurrencyColumnNamePatterns : string []
  member RowVersionTypes : string []
  ...

Full name: EFConcurrencyModeTest.ConcurrencyModeTester

--------------------
new : unit -> ConcurrencyModeTester
val asm : Assembly
type Assembly =
  member CodeBase : string
  member CreateInstance : typeName:string -> obj + 2 overloads
  member EntryPoint : MethodInfo
  member Equals : o:obj -> bool
  member EscapedCodeBase : string
  member Evidence : Evidence
  member FullName : string
  member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
  member GetCustomAttributesData : unit -> IList<CustomAttributeData>
  member GetExportedTypes : unit -> Type[]
  ...

Full name: System.Reflection.Assembly
Assembly.GetAssembly(type: System.Type) : Assembly
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type MyDatabaseEntitiesType = | Dummy

Full name: Index.MyDatabaseEntitiesType
val result : EntityProperty []
member ConcurrencyModeTester.BadConcurrencyModes : edmxFilePath:string -> EntityProperty []
member ConcurrencyModeTester.BadConcurrencyModes : assembly:Assembly -> EntityProperty []
member ConcurrencyModeTester.BadConcurrencyModes : assembly:Assembly * edmxName:string -> EntityProperty []
member ConcurrencyModeTester.BadConcurrencyModes : csdlDocument:System.Xml.Linq.XDocument * ssdlDocument:System.Xml.Linq.XDocument * mslDocument:System.Xml.Linq.XDocument -> EntityProperty []
member ConcurrencyModeTester.BadConcurrencyModes : csdlElement:System.Xml.Linq.XElement * ssdlElement:System.Xml.Linq.XElement * mslElement:System.Xml.Linq.XElement -> EntityProperty []
type Assert =
  static member AreEqual : expected:int * actual:int -> unit + 23 overloads
  static member AreNotEqual : expected:int * actual:int -> unit + 23 overloads
  static member AreNotSame : expected:obj * actual:obj -> unit + 2 overloads
  static member AreSame : expected:obj * actual:obj -> unit + 2 overloads
  static member ByVal : actual:obj * expression:IResolveConstraint -> unit + 2 overloads
  static member Catch : code:TestDelegate -> Exception + 8 overloads
  static member Contains : expected:obj * actual:ICollection -> unit + 2 overloads
  static member Counter : int
  static member DoesNotThrow : code:TestDelegate -> unit + 2 overloads
  static member Equals : a:obj * b:obj -> bool
  ...

Full name: NUnit.Framework.Assert
Assert.IsEmpty(collection: System.Collections.IEnumerable) : unit
Assert.IsEmpty(aString: string) : unit
Assert.IsEmpty(collection: System.Collections.IEnumerable, message: string) : unit
Assert.IsEmpty(aString: string, message: string) : unit
Assert.IsEmpty(collection: System.Collections.IEnumerable, message: string, params args: obj []) : unit
Assert.IsEmpty(aString: string, message: string, params args: obj []) : unit
member ConcurrencyModeTester.FormatBadConcurrencyModes : entityProperties:EntityProperty [] -> string
Fork me on GitHub