Home:ALL Converter>entity framework 6 mysql rowversion

entity framework 6 mysql rowversion

Ask Time:2015-08-20T16:17:02         Author:Ajay Kumar

Json Formatter

I am using entity framework 6 with my sql and seems like row version byte array is not supported by mysql. any help how this can be achieved.

[Column(TypeName = "timestamp")]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime TimeStamp { get; set; }

Author:Ajay Kumar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32113023/entity-framework-6-mysql-rowversion
bubi :

First of all, if you are using automatic migration I think that the property attributes are not enough to create the right field type.\nHere https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html there are the syntax that EF provider should generate to create a timestamp that is automatically generated/updated. \n\nAfter creating the right field type you could do 2 tries: \n\n-Mark the field as Timestamp\n\n[Timestamp]\npublic DateTime TimeStamp { get; set; }\n\n\nI don't think that EF needs that a Timestamp field is byte[]. Timestamp should only mean that is database generated field and that the optimistic concurrency use that field (i.e. update queries contains a where on the keys of the record to update and this field). But often EF does not work as I think...\n\n-Mark the field as generated by database and as a field to use to check for optimistic concurrency exceptions \n\n[ConcurrencyCheck]\n[DatabaseGenerated(DatabaseGeneratedOption.Computed)]\npublic DateTime TimeStamp { get; set; }\n",
2015-08-21T07:03:00
Tony O'Hagan :

Sadly, if you use a DateTime value for optimistic locking it can fail due to loss of precision in the Oracle MySQL driver. There are bug reports on this that are years old that apparently they are refusing to fix. \n\nUpdate: I have just submitted a PR to MySQL .NET Connector v6.9.10 that provides a solution for this issue that provides optimistic locking between EF and non-EF applications. See https://stackoverflow.com/a/50147396/365261 for more details.\n\nAs a workaround, you can create your own Non-DATETIME optimistic locking column.\nBy setting this value via DB trigger (rather in c# code) we ensure that it works for external apps and any DB admin tasks.\n\n\nAdd column rowversion BIGINT NOT NULL DEFAULT 0,\nAdd a trigger to set this field to random or sequential value.\nAdd E6 attributes on the new column.\n\n\nSequential rowversion\n\nCREATE TRIGGER `trg_mytable_before_update` \nBEFORE UPDATE ON `mytable` \nFOR EACH ROW SET NEW.`rowversion` = OLD.`rowversion` + 1;\n\n\nRandom rowversion\n\nCREATE TRIGGER `trg_mytable_before_update` \nBEFORE UPDATE ON `mytable` \nFOR EACH ROW SET NEW.`rowversion` = FLOOR(1 + RAND() * POW(2,54));\n\n\nEF6 Attributes\n\n[DatabaseGenerated(DatabaseGeneratedOption.Computed)]\n[ConcurrencyCheck]\n[Column(\"rowversion\", TypeName = \"bigint\")]\npublic virtual long RowVersion { get; set; }\n",
2018-04-26T02:14:09
yy