zoukankan      html  css  js  c++  java
  • Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database


    The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two clinets update the same entity, one of theirs data will be lost without any notify.

    Some times, the client want to know if his data has been saved successful, so, we have to do some Extra work:

    • Create a project named ConcurrencyTest

    • Add a entity:

        public class Person
        {
            public int PersonId { get; set; }
      
            public string Name { get; set; }
      
            public byte[] RowVersion { get; set; }
        }
      

    Please take attention to the RowVersion property. It used to record the version of the data row in a table.

    Add fluent API codes:

        public class PersonMap : EntityTypeConfiguration<Person>
        {
            public PersonMap()
            {
                Property(p => p.RowVersion)
                    .IsFixedLength()
                    .HasMaxLength(8)
                    .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)
                    .IsRowVersion();
            }
        }
    
    • Add a DbContext:

        public class MyContext:DbContext
        {
            public DbSet<Person> People { get; set; }
      
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Configurations.Add(new PersonMap());
            }
        }
      
    • Add some test codes:

        static void Main(string[] args)
        {
            AddPerson();
            DoConcurrentcyTest();
      
            Console.Read();
        }
      
        static void DoConcurrentcyTest()
        {
            using (MyContext db1 = new MyContext())
            {
                Person p1 = db1.People.Find(1);
      
                using (MyContext db2 = new MyContext())
                {
                    Person p2 = db2.People.Find(1);
                    p2.Name = "Ross";
                    db2.SaveChanges();
                }
      
                p1.Name = "Monnica";
                try
                {
                    db1.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    Console.WriteLine("please refresh it");
                }
            }
        }
      

    When you save the person entity, if someone have changed it after you get, a DbUpdateConcurrencyException will be trowed.

    That's all.

  • 相关阅读:
    flv网页视频播放
    Select显示多级分类列表
    DateTime获取一个月的第一天和最后一天
    服务器不装Excel读取Excel并转换DataTable
    ScriptX使用
    CheckBoxList 全选(jquery版本)
    div绝对定位针对手机浏览器的区别
    JAVA 基础编程练习题22 【程序 22 递归求阶乘】
    JAVA 基础编程练习题21 【程序 21 求阶乘】
    JAVA 基础编程练习题20 【程序 20 求前 20 项之和】
  • 原文地址:https://www.cnblogs.com/zzy0471/p/6911370.html
Copyright © 2011-2022 走看看