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.

Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database的更多相关文章

  1. EF ( Entity Framework) 操作ArcCataLog 生成的(Sql Server)空间数据库

    因为项目需求,现在需要利用EF 操作由Arccatalog生成的sql server空间数据库..在此之前,一直没有接触过空间数据库,在操作空间数据库时 绕了许多弯... 因此写一篇随笔做一个总结. ...

  2. Lerning Entity Framework 6 ------ Defining Relationships

    There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...

  3. 解决VS2010在新建实体数据模型出现“在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误。请与提供程序供应商联系以解决此问题。”的问题

    最近想试着学习ASP.NET MVC,在点击 添加--新建项--Visual C#下的数据中的ADO.NET 实体数据模型,到"选择您的数据连接"时,出现错误,"在 .N ...

  4. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  5. Create a SQL Server Database on a network shared drive

    (原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...

  6. 转载:Restore SQL Server database and overwrite existing database

    转载自:https://www.mssqltips.com/sqlservertutorial/121/restore-sql-server-database-and-overwrite-existi ...

  7. How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络

    SQL Server database administrators may frequently need in especially development and test environmen ...

  8. Visual Studio 2012创建SQL Server Database Project提示失败解决方法

    新建一个SQL Server Database Project,提示: Unable to open Database project This version of SQL Server Data ...

  9. SQL Server Database Backup and Restore in C#

    SQL Server Database Backup and Restore in C# Syed Noman Ali Shah,                          7 Feb 201 ...

随机推荐

  1. SpringData中使用@Modifying注解实现修改操作

    通过@Modifying可以实现修改和删除操作 @Modifying @Query("update Person set email = :email where lastName =:la ...

  2. IOS 发布 升级新版本

    ERROR ITMS-90725: "SDK Version Issue. ERROR ITMS-90725: "SDK Version Issue. This app was b ...

  3. 编织织物的knit course direction and knit wale direction

    来自:http://www.definetextile.com/2013/04/course-wale.html

  4. aptana怎么显示空格 tab和回车等

  5. SOA的理解

    SOA架构的概念网上一大堆,笔者也没有发现一个准确.公认的定义.不过笔者在贴吧了发了一个比较好的解释,能够帮助理解: 一个产品有PC端.iOS端.Android端,有个数据查询的功能.传统的设计方法就 ...

  6. xml文件中&符号需要转义为&amp;

    xml文件中&符号需要转义为&

  7. mysql查询语句中自定义变量(转)

    转:http://blog.sina.com.cn/s/blog_1512521570102wrfl.htmlselect cost,@a:=@a+1 from testone,(select @a: ...

  8. 20175314 实验一 Java开发环境的熟悉

    20175314 实验一 Java开发环境的熟悉 一.实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA 编辑.编译.运行.调试Java程序. 3.完成实验,撰写实验报告,注意实 ...

  9. linux文本格式转换

    问题:在linux环境下面执行SH的可执行文件. -bash: ./start.sh: /bin/sh^M: bad interpreter: No such file or directory 解决 ...

  10. 333. Largest BST Subtree节点数最多的bst子树

    [抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...