[Entity Framework] Code First – Configuring GUID / TimeStamp Properties

GUID (UniqueIdentifier) and TimeStamp are special column types and you can configure by data annotations or Fluent API.

 

1. GUID

Instead of “int“, GUID is used as a key value for a table. In SQL-server, the type name for GUID is “uniqueidentifier“.

If you use GUID as a key, the property maps to the key column by convention.

 

2. Database-Generated Propeties

If the “int” type is used as a key, the column is set as an identity column and increment by 1 whenever new row is added. But when GUID is used as a key, an empty GUID value (all 0) is always used. Therefore you cannot insert more than 2 rows.

The solution is to configure that the new GUID is generated whenever a new row is added.

The [DatabaseGenerated] attribute is used to do this job. You need to set the GUID field to “DatabaseGeneratedOption.Identity“. (For an “int” property, “DatabaseGeneratedOption.Identity” is defaulted.)

Also the “PrimitivePropertyConfiguration” class has the “HasDatabaseGeneratedOption()” method:

  • public PrimitivePropertyConfiguration HasDatabaseGeneratedOption(Nullable<DatabaseGeneratedOption> databaseGeneratedOption)

 

3. TimeStamp

The “TimeStamp” or “RowVersion” columns are used for optimistic concurrency. It is used to verify the currently processing row is in sync with the data saved in the database.

There is no convention for this type of field, so you need to configure the property manually. You can use the byte array as a property type.

The [Timestamp] attribute is used to configure the property. Also the “System.Data.Entity.ModelConfiguration.Configuration.BinaryPropertyConfiguration” has the “IsRowVersion()” method.

  • public BinaryPropertyConfiguration IsRowVersion()

 

4. Checking Concurrency on Non-TimeStamp Fields

You can configure non-TimeStamp fields for concurrency.

The [ConcurrencyCheck] attribute is used to mark the field for currency checking. The “IsConcurrencyToken()” method of the “PrimitivePropertyConfiguration” is used.

  • public PrimitivePropertyConfiguration IsConcurrencyToken()

 

5. Data Annotation Example

public class Product
{
  [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid ProductId { get; set; }

  [Timestamp]
  public byte[] RowTimeStamp { get; set; }

  [ConcurrencyCheck]
  public string SSN { get; set; }
}

 

6. Fluent API Example

public class ProductContext : DbContext
{
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Product>()
      .Property(p => p.ProductId)
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    modelBuilder.Entity<Product>()
     .Property(p => p.RowTimeStamp)
     .IsRowVersion();

    modelBuilder.Entity<Product>()
      .Property(p => p.SSN)
      .IsConcurrencyToken();
  }
}

One thought on “[Entity Framework] Code First – Configuring GUID / TimeStamp Properties

  1. Hi!

    Very good article.
    I would know if I can use hybrid of the Data Annotation and Fluent API.
    Once the Data Annotation I use to reflect on the interface/view and Fluent API I use to Database
    For example:

    public class Product
    {
    [Product Identification]
    public Guid ProductId { get; set; }

    [DisplayName(Product Name)]
    [Required(ErrorMessage= “Product Name is Required”)]
    public string ProductName { get; set; }

    }

    public class ProductContext : DbContext
    {
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity()
    .Property(p => p.ProductId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    modelBuilder.Entity()
    .Property(p => p.Name)
    .HasColumnType(“varchar”)
    .HasMaxLength(100);

    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s