zoukankan      html  css  js  c++  java
  • EF4.1基于数据库生成代码的乐观并发控制

    在需要进行并发控制的书记表中,个TimeStamp类型的列,列名叫TimeStamp也可以

    生成一下代码,找到对应的表生成的代码加个IsConcurrencyToken(true)的属性

       this.Property(t => t.TimeStamp)
        .IsFixedLength()
        .HasMaxLength(8)
        .IsConcurrencyToken(true); //加这句

    目前那个基于数据库的不能生成部分类,所以需要每次生完后再手动设置下.

    以下两段代码都可以看到并发被触发的效果

                JL_MFGContext ctx = new JL_MFGContext();
                var item= ctx.MYTests.FirstOrDefault(ent => ent.ID == 23);

                JL_MFGContext ctx2 = new JL_MFGContext();
                var item2 = ctx2.MYTests.FirstOrDefault(ent=> ent.ID == 23);

           
                item.Title=DateTime.Now.ToString();
                ctx.SaveChanges();

                item2.Title = DateTime.Now.ToString() ;
                ctx2.SaveChanges();

    -----------------------------------

                JL_MFGContext ctx = new JL_MFGContext();
                var item= ctx.MYTests.FirstOrDefault(ent => ent.ID == 23);


           
                item.Title=DateTime.Now.ToString();
                ctx.SaveChanges();

                item.Title = DateTime.Now.ToString() +"2" ;
                ctx.SaveChanges();

                   

  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/wdfrog/p/2218658.html
Copyright © 2011-2022 走看看