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

前端 未结 7 1390
攒了一身酷
攒了一身酷 2021-01-04 13:03

EF defaults to no concurrency control (last write wins) which allows lost updates. Enforcing optimistic concurrency checks can explicitly be configured by setting Concurrenc

7条回答
  •  清酒与你
    2021-01-04 13:16

    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 replacements = new Dictionary()
        {
            { "",
              ""},
    
            { "",
              ""},
    
            { "",
              ""},
    
            { "",
              ""},
        };
    
        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);
            }
        }
    }
    

提交回复
热议问题