ASP.NET Web应用程序+实体框架4.0并发检查 [英] ASP.NET web application + Entity Framework 4.0 with Concurrency Check

查看:143
本文介绍了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.net 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.net 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并发检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆