The title pretty means what i'm trying to do. There is a way to know if an object instance is transient? (Or, at least, was not saved yet?)
Thank you all!
PS.: Not using Exists method.
The title pretty means what i'm trying to do. There is a way to know if an object instance is transient? (Or, at least, was not saved yet?)
Thank you all!
PS.: Not using Exists method.
Usually this is done comparing the value of the entity's PK property with the defined unsaved-value. The most common case is having an int PK without a defined unsaved-value, e.g.:
In this case the unsaved-value is 0, so you can just compare the value of Id with 0 to find out if it's transient or not.
If you want to do this more generically, you can get the unsaved-value from the ActiveRecord model and compare against that using reflection, e.g. (untested!):
This will probably fail in the face of null UnsavedValues (in that case you should compare with the default value of the PK type), but it should get you started.