Skip to content

Commit

Permalink
📝 Timestamp info for Fluent API
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanmiller committed Dec 7, 2015
1 parent c80fd1a commit 4ff91f5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
39 changes: 30 additions & 9 deletions docs/modeling/concurrency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,47 @@ You can use the Data Annotations to configure a property as a concurrency token.
:emphasize-lines: 4
:linenos:

[Timestamp]
Fluent API
----------

You can use the Fluent API to configure a property as a concurrency token.

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/Concurrency.cs
:language: c#
:lines: 5-22
:emphasize-lines: 7-9
:linenos:

Timestamp/row version
---------------------

A timestamp is a property where a new value is generated by the database every time a row is inserted or updated. The property is also treated as a concurrency token. This ensures you will get an exception if anyone else has modified a row that you are trying to update since you queried for the data.

How this is achieved is up to the database provider being used. For SQL Server, timestamp is usually used on a `byte[]` property, which will be setup as a `ROWVERSION` column in the database.

Conventions
^^^^^^^^^^^

You can also use Data Annotations to configure a property as a time stamp. This means that a new value will be generated by the database every time a row is inserted or updated. The property is also treated as a concurrency token. This ensures you will get an exception if anyone else has modified a row that you are trying to update since you queried for the data.
By convention, properties are never configured as timestamps.

Data Annotations
^^^^^^^^^^^^^^^^

How this is achieved is up to the database provider being used. Depending on the provider, it may throw or ignore the annotation on some or all properties. For SQL Server, timestamp is usually used on a `byte[]` property, which will be setup as a `ROWVERSION` column in the database.
You can use Data Annotations to configure a property as a timestamp.

.. literalinclude:: configuring/sample/EFModeling.Configuring.DataAnnotations/Samples/Timestamp.cs
:language: c#
:lines: 11-18
:lines: 11-18
:emphasize-lines: 6
:linenos:

Fluent API
----------
^^^^^^^^^^

You can use the Fluent API to configure a property as a concurrency token.
You can use the Fluent API to configure a property as a timestamp.

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/Concurrency.cs
.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/Timestamp.cs
:language: c#
:lines: 5-22
:emphasize-lines: 7-9
:lines: 5-23
:emphasize-lines: 7-10
:linenos:
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Samples\Timestamp.cs" />
<Compile Include="Samples\InheritanceTphDiscriminator.cs" />
<Compile Include="Samples\Relational\ComputedColumn.cs" />
<Compile Include="Samples\Relational\DefaultValueSql.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Data.Entity;

namespace EFModeling.Configuring.FluentAPI.Samples.Timestamp
{
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(p => p.Timestamp)
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();
}
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public byte[] Timestamp { get; set; }
}
}

0 comments on commit 4ff91f5

Please sign in to comment.