I need to store a time offset in a database column (for example, 12:25 AM, just the time, no date).
I would like to use the nice data visual modeling capabilities in Visual Studio 2008 to generate dbml and the code to go with it. The database is Sql Server 2005.
Since a TimeSpan is essentially an Int64 (aka long) containing the number of ticks, I tried to store that long in the database.
If however I specify that the column should map to a property of type TimeSpan, Visual Studio reports :
Warning : The custom tool ‘MSLinqToSQLGenerator’ failed. Unspecified error
To work around this I’ve mapped the database value to an internal property and I’ve added a partial class that implements property accessors that convert the integer value from the database to TimeSpan and back.
Is there a way to enable the correct code to be generated for this property? Or is there better way to store a timespan in a SqlServer 2005 database that allows easier access using dbml?
Also faced this problem some time ago.
As I understood there is no direct resolution with
SQL Server 2005, so I used a trick:there is
Durationcolumn (which actually should store a .NETTimeStampdata type) in the database of typeBigInt NOT NULL. Here is dbml definition:I set this class property as
protectedwhich mean I don’t want the user of my class access this property directly and made apublicwrapper for this property in partial class:So the user of my class has a easy-to-use property instead of a lot of
longtoTimeSpanconversions.Hope, this helps.