什么流畅的api方法对应于Data Annotations API中的[Timestamp]属性来检查并发性

问题描述 投票:12回答:2

我正在使用Entity Framework 4.1。什么流畅的api方法对应于Data Annotations API中的[Timestamp]属性来检查并发性?

entity-framework-4.1
2个回答
26
投票

如果您有这样的课程:

public class MyEntity
{
    ...
    public byte[] Timestamp { get; set; }
}

您将使用如下的流畅映射:

modelBuilder.Entity<MyEntity>()
            .Property(e => e.Timestamp)
            .IsConcurrencyToken()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

要么:

modelBuilder.Entity<MyEntity>()
            .Property(e => e.Timestamp)
            .IsRowVersion();              

0
投票

您可以使用Ladislav Mrnka描述的代码,但是

两者之间存在细微差别:

modelBuilder.Entity<MyEntity>()
            .Property(e => e.Timestamp)
            .IsConcurrencyToken()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

modelBuilder.Entity<MyEntity>()
            .Property(e => e.Timestamp)
            .IsRowVersion();   

第二个等同于[Timestamp]属性,但是当我们将'[Timestamp]`更改为第一版的流畅API代码时,首先生成以下迁移:

    public override void Up()
    {
        AlterColumn("dbo.MyEntity", "Timestamp", c => c.Binary());
    }

    public override void Down()
    {
        AlterColumn("dbo.MyEntity", "Timestamp", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
    }
© www.soinside.com 2019 - 2024. All rights reserved.