0

In NHibernate when we are mapping an entity, we map version number like this:

Version(x => x.VersionNumber);

As a result, VersionNumber gets added with default value of 1 and with each update of that entity, it auto increments the value and Nhibernate does all this by itself.

Is there anything similar like this in Entity Framework Core?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    If that is just mapping to a numeric VersionNumber column in the DB where the version checking and updating is just internal to Hibernate, I don't know of anything EF does that is similar. Normally for concurrency versioning I'd want to be using a concurrency marker managed by the database itself. For instance with SQL Server the Timestamp data type. EF does support mapping to these as special identity-like columns /w the [Timestamp] attribute. – Steve Py Jan 19 '23 at 06:39
  • 1
    Do you need to keep it numeric? Looks like EF likes to do it with an underlying ` rowversion` column type: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=data-annotations#optimistic-concurrency – David Osborne Jan 19 '23 at 08:38
  • What is the database type used e.g. SQL-Server? – Karen Payne Jan 19 '23 at 12:05
  • @StevePy I was trying to use int as datatype in my code and timestamp in sql server, But that is not allowed in sql server. In nhibernate they handle these concurrency issues and default data type is int for them which is byte for sql server. byte type is not quite readable which is why I wanted int as its type. – rank0001 Jan 20 '23 at 09:33
  • Timestamp in SQL Server is typically mapped to a byte array in C# entities. It is an 8 byte numeric value (like long/ulong) however before you can assume that you need to check the endian of the stored value, especially if you want to use values to send back to EF queries. For in-memory checks where you're using `!=` or `==` between Timestamps, casting them to `long` as an unmapped property is OK, however if you want to pass a timestamp value back into a query such as a `>` or `<` evaluation then see: https://stackoverflow.com/questions/12693934/rowversion-comparison-in-entity-framework – Steve Py Jan 20 '23 at 12:52

0 Answers0