ASP.NET Web应用程序+实体框架4.0并发检查

编程入门 行业动态 更新时间:2024-10-22 15:33:22
本文介绍了ASP.NET Web应用程序+实体框架4.0并发检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个这样的实体:

public class Player { [Required] [Key] public int Id { get; set; } [Required] public string FirstName { get; set; } public string LastName { get; set; } public string NativeCountry { get; set; } [ConcurrencyCheck] public DateTime LastModified { get; set; } public virtual int TeamId { get; set; } //navigational property public virtual Team Team { get; set; } public virtual ICollection<Tournament> Tournaments { get; set; } }

这是我如何配置球员实体:

this is how i configure Player entity:

public PlayerConfiguration() { Property(e => e.Id).IsRequired(); Property(e => e.FirstName).IsRequired().IsConcurrencyToken(true); Property(e => e.NativeCountry).IsOptional(); Property(e => e.LastModified).IsOptional().IsConcurrencyToken(true); HasRequired(e => e.Team).WithMany(s => s.Players).HasForeignKey(f => f.TeamId); }

覆盖OnModelCreating

overridden OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder) { Configuration.ValidateOnSaveEnabled = false; Configuration.LazyLoadingEnabled = true; modelBuilder.Configurations.Add(new PlayerConfiguration()); modelBuilder.Configurations.Add(new TeamConfiguration()); modelBuilder.Configurations.Add(new TournamentConfiguration()); modelBuilder.Entity<Player>().ToTable("Player"); modelBuilder.Entity<Team>().ToTable("Team"); modelBuilder.Entity<Tournament>().ToTable("Tournament"); base.OnModelCreating(modelBuilder); }

地方我这样做是为了更新播放器:

somewhere I do this to update a player:

db.Entry<Player>(player).State = System.Data.EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { }

当我试图在同一时间进行更新任何给定的球员,使用两个浏览器,没有任何反应。我不想使用时间戳注解,因为它会花费我一个额外的柱。如何使用现有的日期时间上次更改列跟踪并发。我甚至试图使名字(及其他)为ConcurrencyToken,却又什么都没有发生。这是如何 [ConcurrencyCheck] 在asp web应用程序的工作。??请帮助..

when I try to update any given player at the same time, using two browsers, nothing happens. I don't want to use a TimeStamp annotation, because it will cost me one extra column. How can I use the existing DateTime LastModified column to track concurrency. I even tried making FirstName (and others) as ConcurrencyToken, but again nothing happened. How does this [ConcurrencyCheck] work in asp web application.?? please help..

推荐答案

看起来那么你正在使用的仍然您code不改变上次更改属性相同的并发令牌。这是一个人之所以使用时间戳列,因为时间戳是自动的数据库和处理你不必处理它。

It looks like your code doesn't change LastModified property so you are using still the same concurrency token. That is a reason why people use Timestamp column because timestamp is automatically handled by database and you don't have to deal with it.

要使用并发令牌你的实体必须遵循非常严格的规定。

To use concurrency token your entity must follow very strict rules.

  • 您的实体,必须持有并发令牌的旧值(在ASP.NET中它需要轮跳闸并发令牌客户和修改数据接收回来的情况下)。
  • 如果你不使用数据库生成并发令牌(时间戳或行版本),你必须手动你要更改记录每次设置
  • 如果您使用分离的实体工作ofter您可以将实体的情况下,否则你将在每次尝试保存更新的数据及时得到异常的新标记可以只设置

下面是示例code,以验证并发检查工作的:

Here is sample code to validate that concurrency checking works:

class Program { static void Main(string[] args) { using (var context = new Context()) { context.Database.Delete(); context.Database.CreateIfNotExists(); context.Players.Add(new Player { FirstName = "ABC", LastName = "EFG", NativeCountry = "XYZ", LastModified = DateTime.Now}); context.SaveChanges(); } using (var context = new Context()) { var player = context.Players.First(); // Break here, go to database and change LastModified date player.LastModified = DateTime.Now; // If you changed LastModified date you will get an exception context.SaveChanges(); } } }

更多推荐

ASP.NET Web应用程序+实体框架4.0并发检查

本文发布于:2023-11-12 11:58:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   实体   框架   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!