I have an entity like this:
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);
}
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)
{
}
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..