Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate setting ConcurrencyMode=Fixed on all RowVersion columns?

EF defaults to no concurrency control (last write wins) which allows lost updates. Enforcing optimistic concurrency checks can explicitly be configured by setting ConcurrencyMode=Fixed on a RowVersion column.

How can we automate setting ConcurrencyMode=Fixed on RowVersion columns in all tables? Having to do this manually when recreating an EF model from a database we risk forgetting it an running without concurrency control.

like image 829
Peter Meinl Avatar asked Oct 04 '12 17:10

Peter Meinl


4 Answers

This is similar to the answer by Mohamed Cassim, but I've updated the code to use XML attribute search and replace, instead of string replace, as the designer can change the order of attributes, or other properties can have different values.

Save this as FixVersionColumnConcurrencyMode.cs, run csc FixVersionColumnConcurrencyMode.cs, and run the resulting FixVersionColumnConcurrencyMode.exe in the same folder as the .edmx file. You can also make it execute post build of the project.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

namespace Utility
{
    internal class FixVersionColumnConcurrencyMode
    {
        private static void Main(string[] args)
        {
            string directoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var files = Directory.GetFiles(directoryPath, "*.edmx");
            foreach (var file in files)
            {
                XDocument xmlDoc = XDocument.Load(file);

                IEnumerable<XElement> versionColumns =
                    from el in xmlDoc.Descendants()
                    where (string)el.Attribute("Name") == "Version"
                    && (string)el.Attribute("Type") == "Binary"
                    && (string)el.Attribute("ConcurrencyMode") != "Fixed"
                    select el;
                bool modified = false;
                foreach (XElement el in versionColumns)
                {
                    modified = true;
                    el.SetAttributeValue("ConcurrencyMode", "Fixed");
                }
                if (modified)
                    xmlDoc.Save(file);
            }
        }
    }
}
like image 76
Narayana Avatar answered Nov 08 '22 07:11

Narayana


Seems like this Feature is not going to be around for EF 5 or EF 6.

I whipped up a quick console app to update the edmx after generating DB First.

Just drop the file in the same directory of your edmx file and run after every regeneration.

Will work for any of the following columns:

RowVersion    timestamp    NOT NULL
rowversion    timestamp    NOT NULL
RowVer        timestamp    NOT NULL
rowver        timestamp    NOT NULL

You can get the console app here https://dl.dropbox.com/u/3576345/EFConcurrencyFixed.exe

or use this piece of code in your own console app.

class Program
{
    static Dictionary<string, string> replacements = new Dictionary<string, string>()
    {
        { "<Property Type=\"Binary\" Name=\"RowVersion\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" />",
          "<Property Type=\"Binary\" Name=\"RowVersion\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" ConcurrencyMode=\"Fixed\" />"},

        { "<Property Type=\"Binary\" Name=\"rowversion\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" />",
          "<Property Type=\"Binary\" Name=\"rowversion\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" ConcurrencyMode=\"Fixed\" />"},

        { "<Property Type=\"Binary\" Name=\"RowVer\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" />",
          "<Property Type=\"Binary\" Name=\"RowVer\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" ConcurrencyMode=\"Fixed\" />"},

        { "<Property Type=\"Binary\" Name=\"rowver\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" />",
          "<Property Type=\"Binary\" Name=\"rowver\" Nullable=\"false\" MaxLength=\"8\" FixedLength=\"true\" annotation:StoreGeneratedPattern=\"Computed\" ConcurrencyMode=\"Fixed\" />"},
    };

    static void Main(string[] args)
    {
        // find all .edmx
        string directoryPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        foreach (var file in Directory.GetFiles(directoryPath))
        {
            // only edmx
            if (!file.EndsWith(".edmx"))
                continue;

            // read file
            var fileContents = System.IO.File.ReadAllText(file);

            // replace lines
            foreach (var item in replacements)
                fileContents = fileContents.Replace(item.Key, item.Value);

            // overwite file
            System.IO.File.WriteAllText(file, fileContents);
        }
    }
}
like image 37
Mohamed Cassim Avatar answered Nov 08 '22 06:11

Mohamed Cassim


In EF6 the designer will set ConcurrencyMode=Fixed on rowversion columns when creating a model from the database.See Designer: Automate setting ConcurrencyMode=Fixed on rowversion columns. Until then we will have to do this manually.

like image 27
Peter Meinl Avatar answered Nov 08 '22 07:11

Peter Meinl


I have not done it myself (yet, but will probably need to soon), but it should be possible to change the tool used for generating code from the .edmx.

Here is an article explaining it for VS2008. I guess the process will be about the same for VS2010 and VS2012.

I'm not currently sure, though, whether this will make it possible to muck around with the ConcurrencyMode in any way.

like image 45
Torben Koch Pløen Avatar answered Nov 08 '22 06:11

Torben Koch Pløen