EF默认为无并发控制(最后写入获胜),允许丢失更新.通过在RowVersion列上设置ConcurrencyMode = Fixed,可以显式配置执行乐观并发检查.
我们如何在所有表中的RowVersion列上自动设置ConcurrencyMode = Fixed?在从数据库重新创建EF模型时必须手动执行此操作,我们可能会忘记它在没有并发控制的情况下运行.
这类似于Mohamed Cassim的答案,但我更新了代码以使用XML属性搜索和替换,而不是字符串替换,因为设计者可以更改属性的顺序,或者其他属性可以具有不同的值.
将其另存为FixVersionColumnConcurrencyMode.cs,运行csc FixVersionColumnConcurrencyMode.cs并在与.edmx文件相同的文件夹中运行生成的FixVersionColumnConcurrencyMode.exe.您还可以使其执行项目的后期构建.
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎此功能不会出现在 EF 5 或 EF 6 中。
在生成 DB First 后,我创建了一个快速控制台应用程序来更新 edmx。
只需将该文件放在 edmx 文件的同一目录中,并在每次重新生成后运行。
将适用于以下任何列:
RowVersion timestamp NOT NULL
rowversion timestamp NOT NULL
RowVer timestamp NOT NULL
rowver timestamp NOT NULL
Run Code Online (Sandbox Code Playgroud)
您可以在此处获取控制台应用程序https://dl.dropbox.com/u/3576345/EFConcurrencyFixed.exe
或在您自己的控制台应用程序中使用这段代码。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5960 次 |
| 最近记录: |