简体   繁体   English

如何将RowVersion属性添加到EF4类

[英]How do I add a RowVersion attribute to a EF4 class

I am using EF4 and creating classes through the Entity design surface then generating the database from them. 我正在使用EF4并通过Entity设计表面创建类,然后从它们生成数据库。 I want to add an attribute to some of the classes to show the timestamp they were last updated. 我想为某些类添加一个属性,以显示它们上次更新的时间戳。

I have added a Version attribute to them, but I don't know which .Net datatype to associate with them so they become either Timestamp or RowVersion in the database when it is generated. 我已经为它们添加了一个Version属性,但是我不知道与它们关联哪个.Net数据类型,因此它们在生成时会成为数据库中的Timestamp或RowVersion。

Any ideas? 有任何想法吗?

You use byte[] type for rowversion/timestamp 您将byte[]类型用于rowversion / timestamp

Example use: http://www.ienablemuch.com/2011/07/using-checkbox-list-on-aspnet-mvc-with_16.html 使用示例: http//www.ienablemuch.com/2011/07/using-checkbox-list-on-aspnet-mvc-with_16.html

If you are in the designer, just type in byte[] or System.Byte[] , I think the field types dropdown selection on EF designer can be typed-in upon. 如果您在设计器中,只需键入byte[]System.Byte[] ,我认为可以输入EF设计器上的字段类型下拉选择。

Given this DDL 鉴于此DDL

create table Movie
(
MovieId int identity(1,1) not null primary key,
MovieName varchar(100) not null unique,
MovieDescription varchar(100) not null unique,
YearReleased int not null,
Version rowversion  -- rowversion and timestamp are alias of each other
);

This is the class mapping: 这是类映射:

public class Movie
{
    [Key]
    public virtual int MovieId { get; set; }
     
    [   Required, Display(Name="Title")
    ]   public virtual string MovieName { get; set; }
     
    [   Required, Display(Name="Description")
    ]   public virtual string MovieDescription { get; set; }
     
    [   Required, Display(Name="Year Released"), Range(1900,9999)
    ]   public virtual int? YearReleased { get; set; }
     
    [Timestamp]
    // byte[] is the rowversion/timestamp .NET type
    public virtual byte[] Version { get; set; }  
 
     
    public virtual IList<Genre> Genres { get; set; }              
}

As far as I know, rowversion is a relative binary value, not an actual time value. 据我所知,rowversion是一个相对二进制值,而不是实际时间值。 It increments for every insert and update that occurs. 它会针对每次发生的插入和更新而递增。 This allows you to compare values to determine which record is newer. 这允许您比较值以确定哪个记录更新。 Since it is relative, given a single rowversion value, you will know nothing, but given two rowversion values, you will know which is older and which is newer, but not by how much. 由于它是相对的,给定一个rowversion值,你什么都不知道,但是给定两个rowversion值,你会知道哪个更旧,哪个更新,但不知道多少。

I don't know which .Net datatype to associate with them so they become either Timestamp or RowVersion in the database when it is generated. 我不知道与它们关联哪个.Net数据类型,因此它们在生成时会成为数据库中的Timestamp或RowVersion。

I'm not sure, but most likely there isn't a datatype that will give you rowversion when going from model to database. 我不确定,但很可能没有一种数据类型可以在从模型到数据库时为你提供rowversion。 You will have to change the database field type yourself, or add the field to the DB and bring the record up to your model. 您必须自己更改数据库字段类型,或将字段添加到数据库并将记录添加到模型中。 You could also generate the DDL and then modify it before creating the DB with it. 您还可以生成DDL,然后在使用它创建数据库之前对其进行修改。

There is also another method where you can extend the functionality of the EF process by deriving from it's classes. 还有另一种方法,您可以通过从它的类派生来扩展EF过程的功能。 You can then choose it yourself, but I'm not too familiar with how to do that. 然后你可以自己选择它,但我不太熟悉如何做到这一点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM