並行語彙基元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 API,若要將屬性設定為並行語彙基元。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 API 來設定的屬性為時間戳記。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; }
}