5

I am trying to implement the solution given in Handling Concurrency with the Entity Framework in an ASP.NET MVC Application .

The article says:

Adding a Tracking Property to the Department Entity

In Models\Department.cs, add a tracking property:

[Timestamp] 
public Byte[] Timestamp { get; set; }

The Timestamp attribute specifies that this column will be included in the Where clause of Update and Delete commands sent to the database.

Since I'm using a model first approach, I followed steps 1 - 5 outlined in Creating a Timestamp column with Entity Framework

  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

When I update my code from the database model, the Models\Department.cs now contains

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

Then I used the metadata class to specify the Timestamp attribute:

// Metadata for Department entity
public class DepartmentMetadata
{
    [Timestamp]
    public byte Timestamp { get; set; }
}

Q1. I tested whether the value of the Timestamp column is changing in case of row edits. It isn't.

EDIT 1 The problem was caused because the SQL Server column type was binary, where it should have been of type timestamp. Changing the data type fixed the issue of the Timestamp column not getting updated.

Q2. I was trying to modify the same entity (using "Open in new tab") to see if an OptimisticConcurrencyException is thrown. It isn't. What am I doing wrong? Please help me understand, thanks.

Screenshot of my <code>Timestamp</code> property

4

1 回答 1

3

问题是因为我每个 HTTP 会话都有一个 ObjectContext,因此打开一个新选项卡并对其进行测试不会导致并发异常。

于 2012-05-22T12:37:14.310 回答