ASP.NET CORE Concurrency Tokens

2018-01-26 10:31:05来源:oschina作者:donald121人点击

分享

For example we may want to configure LastNameon Person to be a concurrency token. This means that if one user tries to save some changes to a Person , but another user has changed the LastNamethen an exception will be thrown. This may be desirable so that your application can prompt the user to ensure this record still represents the same actual person before saving their changes.


Data Annotations


public class Person
{
public int PersonId { get; set; }
[ConcurrencyCheck]
public string LastName { get; set; }
public string FirstName { get; set; }
}

Fluent API


class MyContext : DbContext
{
public DbSet People { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(p => p.LastName)
.IsConcurrencyToken();
}
}
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
Timestamp/row version

A timestamp is a property where a new value is generated by the database every time a row is inserted or updated. The property is also treated as a concurrency token. This ensures you will get an exception if anyone else has modified a row that you are trying to update since you queried for the data.


Data Annotations


public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }1516931209
public byte[] Timestamp { get; set; }
}

Fluent API


class MyContext : DbContext
{
public DbSet Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(p => p.Timestamp)
.IsRowVersion();
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public byte[] Timestamp { get; set; }
}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台