並行語彙基元Concurrency Tokens

注意

此頁面說明如何設定並行語彙基元。This page documents how to configure concurrency tokens. 請參閱處理並行存取衝突的並行存取控制的 EF Core 及如何處理您的應用程式中的並行存取衝突的範例的運作方式的詳細說明。See Handling Concurrency Conflicts for a detailed explanation of how concurrency control works on EF Core and examples of how to handle concurrency conflicts in your application.

屬性設定為並行語彙基元可用於實作開放式並行存取控制。Properties configured as concurrency tokens are used to implement optimistic concurrency control.

慣例Conventions

依照慣例,屬性會永遠不會設定為並行語彙基元。By convention, properties are never configured as concurrency tokens.

資料註釋Data Annotations

您可以使用資料註解將屬性設定為並行語彙基元。You can use the Data Annotations to configure a property as a concurrency token.

public class Person
{
    public int PersonId { get; set; }

    [ConcurrencyCheck]
    public string LastName { get; set; }

    public string FirstName { get; set; }
}

Fluent APIFluent API

您可以使用 fluent 應用程式開發的應用程式開發介面,將屬性設定為並行語彙基元。You can use the Fluent API to configure a property as a concurrency token.

class MyContext : DbContext
{
    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>()
            .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.

如何達成這會決定所使用的資料庫提供者。How this is achieved is up to the database provider being used. SQL Server 的時間戳記通常用在byte [] 屬性,將會安裝為ROWVERSION資料庫中的資料行。For SQL Server, timestamp is usually used on a byte[] property, which will be setup as a ROWVERSION column in the database.

慣例Conventions

依照慣例,屬性會永遠不會設定為時間戳記。By convention, properties are never configured as timestamps.

資料註釋Data Annotations

您可以使用資料註解屬性設定為時間戳記。You can use Data Annotations to configure a property as a timestamp.

public class Blog
{
    public int BlogId { get; set; }

    public string Url { get; set; }
    
    [Timestamp]
    public byte[] Timestamp { get; set; }
}

Fluent APIFluent API

您可以使用 fluent 應用程式開發的應用程式開發介面的屬性設定為時間戳記。You can use the Fluent API to configure a property as a timestamp.

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property(p => p.Timestamp)
            .IsRowVersion();
    }
}

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