【问题标题】:How do I define RowVersion in a C# class when using SQL Server and Entity Framework使用 SQL Server 和实体框架时如何在 C# 类中定义 RowVersion
【发布时间】:2013-03-07 13:55:13
【问题描述】:

我已经创建了一个 SQL 表和一个 EF Fluent 映射如下:

CREATE TABLE [dbo].[Application] (
    [ApplicationId]   INT            IDENTITY (1, 1) NOT NULL,
    [Name] NVARCHAR (50) Not NULL,
    [DataVersion] ROWVERSION,
    CONSTRAINT [PK_dbo.Application] PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
);

我的 EF Fluent API 如下所示:

public class ApplicationConfiguration : EntityTypeConfiguration<Application>
    {
        public ApplicationConfiguration()
        {
            Property(a => a.Name)
                .IsRequired()
                .HasMaxLength(35);

            Property(p => p.RowVersion).IsRowVersion();

        }
    }

我的班级看起来像这样:

public class Application
{
    public int ApplicationId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<TestAccount> TestAccounts { get; set; }
    xxxxxxx
}

谁能告诉我如何在我的班级中定义 RowVersion?

【问题讨论】:

  • 你用的是什么版本的EF?

标签: sql-server asp.net-mvc entity-framework


【解决方案1】:

这对我有用 - 我正在使用 EF4:

public class Application
{
    public int ApplicationId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<TestAccount> TestAccounts { get; set; }

    public virtual byte[] RowVersion { get; set; }
}

映射:

public class ApplicationConfiguration : EntityTypeConfiguration<Application>
{
    public ApplicationConfiguration()
    {
        Property(a => a.Name)
            .IsRequired()
            .HasMaxLength(35);

        Property(p => p.RowVersion).HasColumnName("DataVersion").IsRowVersion();
    }
}

PS:我的数据库colum的数据类型是timestamp(我使用的是SQL Server 2005)

【讨论】:

    【解决方案2】:

    我觉得是

    public class Application
    {
        public int ApplicationId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<TestAccount> TestAccounts { get; set; }
        public virtual byte[] DataVersion { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多