views:

23

answers:

1

For some reason the TimeSpan property on my class is not being persisted into the database by Subsonic it is simply being ignored!? All other properties are being saved OK. I am using SimpleRepository and RunMigrations, Subsonic v3.0.0.3.

public TimeSpan Time { get; set; }

Are TimeSpans not supported?

+2  A: 

TimeSpan is not a valid 2005/2008 SQL data type.

Store it as a numeric based SQL data type. Convert your TimeSpan to an appropriate duration based on the accuracy you require:

// Define an interval of 1 day, 15+ hours.
TimeSpan interval = new TimeSpan(1, 15, 42, 45, 750); 
Console.WriteLine("Value of TimeSpan: {0}", interval);
Console.WriteLine("{0:N5} minutes, as follows:", interval.TotalMinutes);

Beware that there is a distinct difference between interval.Minutes and interval.TotalMinutes!

http://msdn.microsoft.com/en-us/library/system.timespan.totalminutes.aspx

Thus:

// my duration
int duration = Time.TotalMinutes;
// now insert this into the database
Junto
Thanks but as I am using SimpleRepository would that mean having to change the data type of my object purely for the purpose of inserting the data in the database and then casting it everytime I want to use it - unless I am misunderstanding?
ProNotion
Hi ProNotion. My bad. I misread your question. Another user has a similar (ActiveRecord rather than SimpleRepository) TimeSpan conversion issue and the answer from John Sheehan might relate to your issue: http://stackoverflow.com/questions/1448626/error-with-subsonic-attempting-to-convert-timespan-to-stringMaybe you should raise this question on the SubSonic Google Group: http://groups.google.com/group/subsonicproject
Junto
On reflection your recommendation works fine for me by storing an integer representation of the number of milliseconds in the TimeSpan. Thanks
ProNotion