简体   繁体   English

使用SQL Server和Entity Framework时,如何在C#类中定义RowVersion

[英]How do I define RowVersion in a C# class when using SQL Server and Entity Framework

I have created a SQL table and an EF Fluent mapping as follows: 我创建了一个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)
);

My EF Fluent API looks like this: 我的EF Fluent API如下所示:

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

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

        }
    }

My Class looks like this: 我的班级看起来像这样:

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

Can someone tell me how I can define the RowVersion in my class? 有人能告诉我如何在班上定义RowVersion吗?

This works for me - I'm using EF4: 这对我有用 - 我正在使用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; }
}

Mapping: 制图:

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

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

PS: The datatype of my database colum is timestamp (I'm using SQL Server 2005) PS:我的数据库列的数据类型是时间戳 (我使用的是SQL Server 2005)

I think it's 我想这是

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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从SQLDataReader到C#变量读取Rowversion或时间戳记SQL Server数据类型 - How Do I Read A Rowversion or Timestamp SQL Server Data Type From a SQLDataReader to a C# Variable 当我将bool映射到带有Entity Framework的SQL Server时,如何定义SQL Server数据类型? - How do I define a SQL Server datatype when I have a bool mapped to SQL Server with Entity Framework? 如何使用 Oracle 和 SQL Server 在 .NET 4.5 C# Entity Framework 6 中将列映射到大写? - How do I map a column to uppercase in .NET 4.5 C# Entity Framework 6 using both Oracle and SQL Server? 如何使用C#和实体框架实现SQL Server分页? - How to implement SQL Server paging using C# and entity framework? 如何在MVC 4中使用Entity Framework从C#代码部分使用SQL函数 - How do I use a SQL function from C# code section using Entity Framework in MVC 4 是否可以使用实体框架将SQL Server的rowversion类型映射到比byte []更友好的内容? - Is it possible to map SQL Server's rowversion type to something friendlier than byte[] using Entity Framework? 使用实体框架的SQL Server DateTime对象和C#Datetime - SQL Server DateTime Object and C# Datetime using Entity Framework 如何使用C#实体框架获取SQL Server表/视图等中每一列的数据类型? - How can I get data type of each column in a SQL Server table/view etc. using C# Entity Framework? 如何使用C#和Entity Framework在WPF中备份和还原SQL Server - How to backup and restore SQL Server in WPF with C# and Entity Framework 使用EF代码优先时,SQL Server中的rowversion / timestamp列是否需要非聚集索引? - Do rowversion/timestamp columns in SQL Server need a nonclustered index when using EF code-first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM