Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

Any ideas?

like image 441
Colin Desmond Avatar asked Apr 05 '10 19:04

Colin Desmond


1 Answers

You use byte[] type for rowversion/timestamp

Example use: 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.

Given this 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; }              
}
like image 58
Michael Buen Avatar answered Oct 13 '22 09:10

Michael Buen