How do I tell Linq To SQL to specify DateTime values to be of Kind: DateTimeKind.Utc? RRS feed

  • Question

  • Hello,

    What is the easiest way to tell Linq to SQL to assume DateTime values to be in UTC and when instantiating entities?


    I have an entity Appointment that has property/column StartDate and EndDate. There is no time zone specification of these values in the database, so if I use the Visual Studio modelling tool for Linq to Sql to generate the entity classes the default is that DateTime values are instantiated without TimeZone specification thus this is true: dateTime.Kind == DateTimeKind.Unspecified.

    So the question is, can I tell Linq to Sql to instantiate all entities so that this will be true: dateTime.Kind == DateTimeKind.Utc?


    Tuesday, April 14, 2009 1:17 PM

All replies

  • Hi Again,

    So I guess one way to do it would be to implement a partial method for the date property. If the the propertyName is StartDate I could implement the partial method OnStartDateChanging(DateTime value), and take handle the different cases (value.Kind being Utc, Local or Unspecified).

    The problem is that I am generating the code with the VisualStudio modelling tool for Linq to SQL. And per default the generated code for the property has the attribute Column(Storage="_StartDate", ...) Which results in LinqToSql bypassing the setter and the partial method will never be called.

    I would like to not have to modify the generated code.

    So my question now is, how do I tell the modelling tool to not bypass the setter for this property?

    Tuesday, April 14, 2009 2:29 PM
  • Not sure if this helps... it is related to using Linq to SQL with Silverlight 3 RIA services:

    "Details on DomainService.UsesUtcDateTimes: Our LinqToEntities and LinqToSql domain services override the base implementation (which returns true), both returning false, since that is the behavior we deemed most common. This means that the LTS/LTE domain services are doing conversions to Local time. You can override this property and return true if you're storing UTC times in your DB and you don't want any conversions. If you override and set UsesUtcDateTimes = true, our serializer won't perform any conversions to the time read from the datasource."


    Tuesday, May 5, 2009 4:31 AM
  • Hello, and that's what's actually NOT working - the DateTime returned from the RIA DomainService is ALWAYS crippled to local timezone, although it's UTC. The UsesUtcDateTimes is simply ignored - and if you google, you'll know what I mean :-(.. Please, fix this bug. Thank you, Andrej
    Friday, August 28, 2009 11:07 AM