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 ...
随机推荐
- 最适合入门的Laravel中级教程(三)表单验证
做开发有个原则是永远不能信任用户输入的数据: 即便前端已经做了验证: 在后端 php 也必须要再次验证: laravel 为表单验证提供了强大且简单的方案: 创建示例路由: routes/web.ph ...
- Python常用字符编码
字符编码的常用种类介绍 第一种:ASCII码 ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一 ...
- django内置分页功能扩展
实现自定制页码数类型class myPaginator(Paginator): def __init__(self,curr_page,per_page_num,*args,**kwargs): se ...
- Pandas重塑和轴向旋转
重塑和轴向旋转 Se import pandas as pd import numpy as np from pandas import Series data=pd.DataFrame(np.ara ...
- JavaScript实现观察者模式
首先: 观察者模式是软件设计模式的一种,它
- js中遇到的一些方法和函数
这是一个笔记文章,方便日后复习. 加号的优先级高于三目运算符: console.log(') ? 'define' : 'undefine');//define setTimeout(code,mil ...
- mysql学习笔记--表操作
一.显示所有表 1. 语法:show tables; 二.创建表 1. 语法:create table [if not exists] 表名( 字段名 数据类型 [null | not null] ...
- 定时任务APScheduler
安装 APScheduler $ pip install apscheduler 快速开始 from apscheduler.schedulers.blocking import BlockingSc ...
- android 隐藏虚拟按钮栏及标题等权限设置
华为手机有虚拟按钮,根据以下设置方法可以进行隐藏控制 /** * 隐藏虚拟按键,并且全屏 */ protected void hideBottomUIMenu(Context context){ if ...
- git的一些常用操作
一.克隆默认分支 git clone 远程地址 二.克隆某一分支 git clone -b 分支名 远程地址 三.切换分支 git branch 分支名 四.一般开发提交的流程 1).查看本地状态 g ...