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.
Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database的更多相关文章
- EF ( Entity Framework) 操作ArcCataLog 生成的(Sql Server)空间数据库
因为项目需求,现在需要利用EF 操作由Arccatalog生成的sql server空间数据库..在此之前,一直没有接触过空间数据库,在操作空间数据库时 绕了许多弯... 因此写一篇随笔做一个总结. ...
- 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 ...
- 解决VS2010在新建实体数据模型出现“在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误。请与提供程序供应商联系以解决此问题。”的问题
最近想试着学习ASP.NET MVC,在点击 添加--新建项--Visual C#下的数据中的ADO.NET 实体数据模型,到"选择您的数据连接"时,出现错误,"在 .N ...
- 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 ...
- 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 ...
- 转载:Restore SQL Server database and overwrite existing database
转载自:https://www.mssqltips.com/sqlservertutorial/121/restore-sql-server-database-and-overwrite-existi ...
- 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 ...
- Visual Studio 2012创建SQL Server Database Project提示失败解决方法
新建一个SQL Server Database Project,提示: Unable to open Database project This version of SQL Server Data ...
- SQL Server Database Backup and Restore in C#
SQL Server Database Backup and Restore in C# Syed Noman Ali Shah, 7 Feb 201 ...
随机推荐
- 04 Python数据类型
Python 数据型1. int: 1,2,3 ....2. bool: True False3. str: 存贮少量数据 'asjkdh','工查'4. list: 列表,存贮大量数据 [1,2,3 ...
- jeecg开源项目的IDEA的部署
JEECG采用了SpringMVC + Hibernate + Minidao(类Mybatis) + Easyui(UI库)+ Jquery + Boostrap + Ehcache + Redis ...
- Elasticsearch **代码片段
```JAVA BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); RangeQueryBuilder createTimeQuery ...
- docker mysql Exit 1
用laradock启动mysql时,state总是 Exit 1 ,docker-compose build后也没有效果 这时应该在-/.laradock/data(.env的DATA_PATH_HO ...
- Linux的Namespace与Cgroups介绍
Namespace 的概念 Linux Namespace 是kernel 的一个功能,它可以隔离一系列系统的资源,比如PID(Process ID),User ID, Network等等.一般看到这 ...
- cmd创建文件命令
一.建立空文件的几种方法 1.cd.>a.txt cd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出. >表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.t ...
- type的解释
在jquery-19.1.1源码中,type,检查对象的类型是:Boolean/Number/String/Function/Array/Date/RegExp/Object/Error中的一种,返回 ...
- springmvc 怎么响应json数据
springmvc 怎么响应json数据@Controller@RequestMapping("/items") class ItemsController{ @RequestM ...
- ABAP 省市县级联搜索帮助
在展示ABAP代码之前,先建立自建表ZCHENH006,表中包含两个关键字段 BELNR(地区编码),SDESC(地区描述). 编码规则参考:身份证前六位地区编码规则,可参考我另外一篇Blog导入系统 ...
- 十五、Facade 窗口设计模式
需求:让复杂的事务看起来简单 原理: 代码清单: DataBase: public class DataBase { private DataBase(){} public static Proper ...