MySQL Timestamp default precision mapping issue

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
SerhiiS
Posts: 7
Joined: Thu 01 Apr 2021 19:28

MySQL Timestamp default precision mapping issue

Post by SerhiiS » Thu 01 Apr 2021 20:11

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: MySQL Timestamp default precision mapping issue

Post by Shalex » Mon 05 Apr 2021 10:38

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.

SerhiiS
Posts: 7
Joined: Thu 01 Apr 2021 19:28

Re: MySQL Timestamp default precision mapping issue

Post by SerhiiS » Tue 06 Apr 2021 16:56

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: MySQL Timestamp default precision mapping issue

Post by Shalex » Wed 07 Apr 2021 14:05

Why doesn't "Found Rows=true;" suit your scenario?

SerhiiS
Posts: 7
Joined: Thu 01 Apr 2021 19:28

Re: MySQL Timestamp default precision mapping issue

Post by SerhiiS » Wed 07 Apr 2021 14:43

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?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: MySQL Timestamp default precision mapping issue

Post by Shalex » Fri 09 Apr 2021 10:13

We do not know a better solution to the case you have encountered.

SerhiiS
Posts: 7
Joined: Thu 01 Apr 2021 19:28

Re: MySQL Timestamp default precision mapping issue

Post by SerhiiS » Wed 14 Apr 2021 18:10

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)

Post Reply