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.
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);
}
}
}
}
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);
}
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With