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 ...
随机推荐
- leetcode75
class Solution { public: void sortColors(vector<int>& nums) { sort(nums.begin(), nums.end( ...
- RocketMQ入门(Filter)_5
RocketMQ中存储的消息对于消费者来说,并不完全都是他们需要的,因此需要对消息进行过滤. 订阅Topic主题 ,选择Tags都是我们简单的过滤.Topic是大分类,Tags是二级分类. Rocke ...
- 下载安装 STS(Spring Tool Suite),推荐对应 Eclipse 版本号,适用于Windows32位(xp、2003)
sts下载地址:https://spring.io/tools/sts/legacy 虽然sts内置了版本对应的eclipse,仍推荐使用当前环境下稳定使用的eclipse版本. Start 找到ec ...
- 远程桌面 把explorer关掉了
用Ctrl+Alt+End调出远程桌面的任务管理器.然后,运行explorer.exe即可重启该服务.
- Html转成Image
html转图片,原来用了html2image.jar,但是做不到复杂的css js渲染效果. 在网友推荐下,可以用wkhtmltoimage插件,这是用谷歌浏览器的内核webkit,网上搜一下可以下载 ...
- Understanding ABI Files
[Understanding ABI Files] ABI files can be generated using the eosio-cpp utility provided by eosio.c ...
- JS 操作样式 style
1. 任何支持 style 特性的 HTML 元素在 JavaScript 中都对应着有一个 style 属性,指向一个 CSSStyleDeclaration 的一个实例对象,包含该元素的内嵌sty ...
- Pandas重塑和轴向旋转
重塑和轴向旋转 Se import pandas as pd import numpy as np from pandas import Series data=pd.DataFrame(np.ara ...
- 十二 logging模块
一 日志级别 CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INFO = 20 DEBUG = 10 ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...