简体   繁体   中英

Entity Framework Core PostgreSQL Inserting -2147482647 Id

I have a .NET Core 2.0 project that is using a Postgres Database which I am using EntityFrameworkCore.PostgreSQL (2.0.1) which is inserting a strange value for my table Id.

This seems to happen randomly and seems to ignore my ID sequence completely.

It inserts the smallest int32 value -2147482647 as the ID a row that is inserted. Then all future inserts cannot happen and it present this error:

The instance of entity type 'Object' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

Before Entity saves I know it uses a garbage ID which I assume is this negative, but from my understanding when you Save that ID should be updated to match your sequence. And it looks like future inserts are also getting the same garbage ID which is causing the error I am receiving.

Wondering if anyone else has had this issue, and if so what is the cause.

Late answer but for others finding this question: Yes, I've seen this, and in the combination EF Core + PostgreSQL database too. Please ensure that your primary column's sequence value is correct! For us, the "current value" it was somehow incorrectly set so that the next one would hit an already existing id.

What I think is going on here is that this is treated like a special value for new rows but it's exposed to the user when a new row can't be inserted for database reasons. I don't think this is normally Entity Framework Core issues.

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