简体   繁体   中英

How to use T-SQL timestamp with Linq to Entities EF4

I currently have a project where we are trying to migrate some data from a client database and into our central DB store. We are then going to expose the data back to the client via web service methods.

One thing we would like to do is make use of a T-SQL timestamp (or rowversion) column on the data, so the client can check their local version of the data against ours, and for instance call a method saying "give me all the data with a version > 10"

This is proving a little problematic for us, because Entity Framework will interpret the timestamp column as a Byte array, so we can't figure out the best way to write code in LINQ to get all rows of data where version > X where the type of version is Byte[].

In pseudo code

getdata(int checkVersion)
{
  return Shoppers.Where(s=>s.Version > checkVersion).ToList();
}

//==> need to convert the Version column from Byte[] to int somehow in Linq-Entities

One suggestion would be to create a new computed column in the table of type bigint which converts the version (timestamp) column.

I wonder if there is actually a way to do this in LINQ though, without introducing another column into the table?

i think you will like this msdn article

By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated. The Entity Framework saves object changes to the database without checking for concurrency. For entities that might experience a high degree of concurrency, we recommend that the entity define a property in the conceptual layer with an attribute of ConcurrencyMode="fixed", as shown in the following example:

<Property Name="Status" Type="Byte" Nullable="false" ConcurrencyMode="Fixed" />

When this attribute is used, the Entity Framework checks for changes in the database before saving changes to the database. Any conflicting changes will cause an OptimisticConcurrencyException. For more information, see How to: Manage Data Concurrency in the Object Context. An OptimisticConcurrencyException can also occur when you define an Entity Data Model that uses stored procedures to make updates to the data source. In this case, the exception is raised when the stored procedure that is used to perform updates reports that zero rows were updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM