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 ...
随机推荐
- Spring利用注解@Value获取properties属性为null
今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...
- python中__get__,__getattr__,__getattribute__的区别
__get__,__getattr__和__getattribute都是访问属性的方法,但不太相同. object.__getattr__(self, name) 当一般位置找不到attribute的 ...
- openvpn通过ldap或ad统一认证解决方案思路分享
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/986933 缘起:成 ...
- ElasicSearch(4) 与jest结合
https://spring.io/projects/spring-data-elasticsearch https://docs.spring.io/spring-data/elasticsearc ...
- ajax用FormData方式提交
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP和Redis实现在高并发下的抢购及秒杀功能示例详解
抢购.秒杀是平常很常见的场景,面试的时候面试官也经常会问到,比如问你淘宝中的抢购秒杀是怎么实现的等等. 抢购.秒杀实现很简单,但是有些问题需要解决,主要针对两个问题: 一.高并发对数据库产生的压力二. ...
- 权限控制框架---shiro入门
1.shiro控制权限的两种思想:粗粒度url级别,细粒度方法级别 2.shiro执行流程简介 3.案例 3.1shiro控制用户登录实现,登录其实就是shiro中的认证. (1)配置web.xml( ...
- HDU2665 求区间第K大 主席树
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...
- Windows导入EOS工程
[Windows导入EOS工程] 编写 EOS 智能合约时,有许多EOS提供的结构.函数.宏.文档对这些内容的描述毕竟模糊,再多的文档也比不过看实际的代码,所以还要下载EOS代码看一下. 最好有个ID ...
- Appium 学习一:环境搭建问题
1.安装Android-sdk http://tools.android-studio.org/index.php/sdk 问题1:下载 android-sdk_r24.4.1-windows.zip ...