Page 1 of 1

MySQL Timestamp default precision mapping issue

Posted: Thu 01 Apr 2021 20:11
by SerhiiS
Hello,

was not able to found a settings or any other way to fix this by myself, so asking for help:

I have TIMESTAMP field in the mysql db with default precision (which is 0 according to mysql docs).

When code is generated from model it has no ".HasPrecision(0)" call for this db field in the mapping method. This seems to cause error:
"Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s)"
Error raised when there at least 2 calls of dbContext.SaveChanges() during ONE second when there only TIMESTAMP field is changed. As I understand, MySql does not update the db because timestamp value did not change, but ef.core is
expecting 1 affected record.

The TIMESTAMP(X) fields has no such issue because ".HasPrecision(X)" is generated.

Re: MySQL Timestamp default precision mapping issue

Posted: Mon 05 Apr 2021 10:38
by Shalex
Please specify "Found Rows=true;" in your connection string and try again.

The Found Rows connection string parameter determines whether the provider will return the number of rows matched by the WHERE condition of the UPDATE statement instead of the rows actually changed as the number of changed rows.

Re: MySQL Timestamp default precision mapping issue

Posted: Tue 06 Apr 2021 16:56
by SerhiiS
thank you for the answer,

but this option seems more like workaround in this specific case and not real solultion for the TIMESTAMP precision issue.

Correct me if I am wrong.

I can track such fields on my own and cut fraction part for the seconds but this defeat purpose of using entity framework with integrated checks etc. Also I don't know if I can set .HasPrecision(0) for the default timestamp fields without it to be overwritten on the next model's code generation.

Re: MySQL Timestamp default precision mapping issue

Posted: Wed 07 Apr 2021 14:05
by Shalex
Why doesn't "Found Rows=true;" suit your scenario?

Re: MySQL Timestamp default precision mapping issue

Posted: Wed 07 Apr 2021 14:43
by SerhiiS
I am not good at this area but as far as I understand, use of this option will basicaly disable ef.core concurrency control feature as it depend on the real affected number.

Also if entity developer respect the TIMESTAMP(X) precision, why not respect the default precision value also? Am I missing something cruical here? is it considered as bad behaviour?

Re: MySQL Timestamp default precision mapping issue

Posted: Fri 09 Apr 2021 10:13
by Shalex
We do not know a better solution to the case you have encountered.

Re: MySQL Timestamp default precision mapping issue

Posted: Wed 14 Apr 2021 18:10
by SerhiiS
was able to fix my case using code below (example only for timestamp type):

Code: Select all

partial void CustomizeMapping(ref ModelBuilder modelBuilder)
{
	var _modelBuilder = modelBuilder;
	_modelBuilder.Model.GetEntityTypes().ToList().ForEach(e => {
		e.GetProperties().Where(p=>p.GetColumnType()=="timestamp" && p.GetPrecision()==null).ToList().ForEach(p =>
		{
			_modelBuilder.Entity(e.ClrType).Property(p.Name).HasPrecision(0);
		});
	});
}
still thinking it is quite important feature for mysql version of Entity Developer. Would be great to have an option for entity developer to generate (or not) default precision value for specific types (datetime/time/timestamp)