Hi,
I'm using EF Core v1.0.
I've an entity with int (auto increment) PK. When I add this entity to DbSet, it's Id is set to a negative value like -2147482647. This value is may be slightly different but close to that number. After SaveChanges, it gets the database generated value as normally.
I was expecting it remain "0" until savechanges as like EF 6.x.
You can re-produce that by implementing this sample for example: https://docs.efproject.net/en/latest/platforms/full-dotnet/new-db.html
Is that an expected behaviour? If yes, why?
Thanks.
It seems like the replacement for null is int.MinValue ? (that is, -2,147,483,648)
I think DateTime is handled the same way.
If this is a problem to you, you may use public int? Id { get; set; } instead.
(note that I don't know the exact reasoning behind the behavior, that's why I can't answer your question!)
Hi,
Thanks for your answers.
Many application check if (Id == 0) to understand if the entity is saved to database or not. So, this breaks our existing code. Surely, we can change if (Id <= 0) but I could not understand why it's set to a negative value like that.
@hikalkan EF Core generates temporary IDs which are then replaced with the real IDs when SaveChanges is called.
OK, while I did not like this design much I will change my code to handle it.
Thanks for your answers.
Why does it do that?
2016-07-25 18:27 GMT+02:00 Arthur Vickers [email protected]:
Closed #6147 https://github.com/aspnet/EntityFramework/issues/6147.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/EntityFramework/issues/6147#event-733773242,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAoyANUSdlnN_HL9lt2nqCZLX_4blxMAks5qZOPpgaJpZM4JTVbV
.
I encountered the same but using public long Id {get;set;}. The value is close to minValue of long. But i have an exception thrown, that says that the Id cannot be NULL or 0.
Does the primary key for autoincrement and to enable entity framework to handle the ids automatically NEEDS to be int?
@pollingerMaxi This issue is closed. If you are hitting something that you think is a bug, then please file a new issue with a runnable project/solution or full code listing that reproduces what you are seeing.
@hikalkan EF Core generates temporary IDs which are then replaced with the real IDs when SaveChanges is called.
Hi @hikalkan, do you know where that is documented? thanks!
I don't remember if it was documented, I've found it by debugging.
@hikalkan @ajcvickers this was discontinued right? Temporary key values are no longer set onto entity instances
@brunomartinspro Yes. Negative values are still generated as temporary IDs, but they are not stored on entity instances.
Most helpful comment
@hikalkan EF Core generates temporary IDs which are then replaced with the real IDs when SaveChanges is called.