How to adjust the setting of EF 4.3.1. Rowversion line changed

Let's say that we have:

public class Driver
{
    public int driverID { get; set; }
    public byte[] stamp { get; set; }
    public string name { get; set; }
    public string prename { get; set; }
}

In short, I am faced with such a situation.

 ...
 var myDriver = myCustomDBContext.Drivers.AsNoTracking()
                                         .Where(d => d.driverID == driverID)
                                         .SingleOrDefault();
 ...
 myDriver.name = "John";
 myDriver.prename = "Lennon";
 ...
 myCustomDBContext.Drivers.Attach(myDriver);
 myCustomDBContext.Entry(myDriver).State = EntityState.Modified;
 myCustomDBContext.SaveChanges();
 ...

And the result

 The column cannot be modified because it is an identity, rowversion or 
     a system column. [Column name = stamp]

Is there any method that could cause an update for a single object, or the workaround for this rowversion column should not be set as Modified.

+5
source share
1 answer

It looks like you did not specify the property stampas a string in your model, and this is just a binary field. You can specify it with the Fluent API:

modelBuilder.Entity<Driver>().Property(d => d.stamp)
    .IsRowVersion()
    .IsConcurrencyToken(false);

, stamp concurrency. ( rowversion - concurrency, .) concurrency, Fluent API...

modelBuilder.Entity<Driver>().Property(d => d.stamp)
    .IsRowVersion();

... :

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

EF UPDATE .

Edit

Database-First, [Timestamp] . Code-First.

Database-First, , EDM, EDMX:

connectionString="metadata=res://*/Model1.csdl
                          |res://*/Model1.ssdl
                          |res://*/Model1.msl;
                          ...
                          ..."

Entity Framework , - Fluent API (OnModelCreating ). EDMX.

, stamp concurrency, EDMX. XML :

SSDL:

<Property Name="stamp" Type="timestamp" Nullable="false"
          StoreGeneratedPattern="Computed" />

CSDL:

<Property Name="stamp" Type="Binary" Nullable="false" MaxLength="8"
          FixedLength="true"
          annotation:StoreGeneratedPattern="Computed"
          ConcurrencyMode="Fixed" />

Visual Studio: stamp , "Concurrency Mode" "Fixed" ( "StoreGeneratedPattern" "Computed" ).

. , . Fluent API , EDMX .

+7

All Articles