0

I'm using EF - Model First for saving my entities. I've added a column "RowVersion" with the options:

enter image description here

In a metadata class I added the annotation [Timestamp].

But when I add/insert an entity in the table the property RowVersion stays null. Now the insert won't because I set the column nullable but otherwise it keeps running into an exception telling me that RowVersion is NULL.

How could I solve this?

Xepos
  • 167
  • 1
  • 15

1 Answers1

1

I am seeing the exact same behavior. I have worked on this all afternoon and the only thing that I was able to get to work was to implement the solution at this link: http://www.undisciplinedbytes.com/2012/03/creating-a-timestamp-column-with-entity-framework/

Here are the steps copied from the link:

Timestamp column with model-first approach

Now, setting this column with the model-first approach is a little bit trickier. There’s no built-in support for this in EF’s model itself (yet), so we’ll have to hack the code generation template to fulfill our needs.

What we need to do to set up a timestamp column using model first approach is the following:

  1. Add a property named “Timestamp” to the entity in EF’s model
  2. Set the type to binary
  3. Set nullable to false
  4. Set StoreGeneratedPattern to Computed
  5. Set ConcurrencyMode to Fixed
  6. Create a copy of SSDLToSQL10.tt (typically found in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen)
  7. Edit the line that says:

[<#=Id(prop.Name)#>] <#=prop.ToStoreType()#> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>

Change it to:

[<#=Id(prop.Name)#>] <#if (string.Compare(prop.Name,"TimeStamp",true) == 0) { #>TIMESTAMP<# } else { #><#=prop.ToStoreType()#><# } #> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>

This will change any column that is called “Timestamp” (case insensitive) to be a Timestamp column.

  1. Click on the entity canvas and set the DDL Generation Template to this new copy of the file
  2. Click on Generate Database From Model 10.Enjoy your new concurrency-aware data access!
Kerry Jenkins
  • 727
  • 3
  • 7
  • 9