如何自动设置ConcurrencyMode =在所有RowVersion列上修复? [英] How to automate setting ConcurrencyMode=Fixed on all RowVersion columns?

查看:109
本文介绍了如何自动设置ConcurrencyMode =在所有RowVersion列上修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF默认为无并发控制(最后写入胜利),允许丢失更新。
执行乐观并发检查可以通过在RowVersion列上设置ConcurrencyMode = Fixed来显式配置。

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.

我们如何自动设置ConcurrencyMode =在所有的RowVersion列中修复表?
在从数据库重新创建一个EF模型时,需要手动执行此操作,我们有可能忘记一个没有并发控制的运行。

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.

推荐答案

p>这与Mohamed Cassim的答案相似,但是我已经更新了使用XML属性搜索和替换的代码,而不是字符串替换,因为设计人员可以改变属性的顺序,或者其他属性可以有不同的值。

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.

将其另存为 FixVersionColumnConcurrencyMode.cs ,运行 csc FixVersionColumnConcurrencyMode.cs ,并将生成的FixVersionColumnConcurrencyMode.exe与.edmx文件相同的文件夹中运行。您也可以执行项目的后期制作。

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);
            }
        }
    }
}

这篇关于如何自动设置ConcurrencyMode =在所有RowVersion列上修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆