I have an object I´m trying to save that has a DateTime property.
If I leave the property to DateTime.MinValue SQL Server 2005 gives me an error, so I did the following code in the Save method of my program:
Code:
foreach (PropertyInfo pi in objectToSave.GetType().GetProperties()) {
if (pi.PropertyType == typeof(DateTime)) {
if (((DateTime)pi.GetValue(objectToSave, null)) == DateTime.MinValue) {
pi.SetValue(objectToSave, DateHelper.MinValue, null);
}
}
}
This way I assure that no DateTime properties that are currently null will get saved as DateTime.MinValue in the database. I did the opposite in the Load method, so it sets the property do DateTime.MinValue in case the database returns the DateHelper.MinValue property value.
This works well until I have the following situation:
BeginTrans();
object id = Save(project);
Project proj = Load(id);
Commit();
The problem here is when I Save the object it has DateHelper.MinValue for all date properties. This works perfectly if I comment the load line (the object gets saved perfectly).
But if I load the same Id from the database in the same transaction the object (another instance if you look at the code above) has the DateTime properties set again to DateTime.MinValue (desired behavior).
By the time I commit it gives me an error of Date must be higher than ....
What I mean is: If I load a not-commited object from the database and modify it in memory, by the time I commit the whole transaction the object that will be saved is the loaded one? Shouldn´t the object being saved be the one from the save line?!
Maybe I am missing something here, but I don´t think I am.
Thanks anyway,
Bernardo Heynemann