我有一个这样的实体:
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并发检查
发布评论