Search code examples
sqllinq-to-sqldatetimetypessqlmetal

System.DateTime and Sql datetime2 in Linq2Sql (using sqlmetal)


I'd like to utilize new Sql datetime2 data type for event logging (as standard datetime has lower precision than System.DateTime causing data loss on storing) but when i generate the code with sqlmetal.exe i get the following warning:

db.dbml(98) : Warning DBML1008: Mapping between DbType 'DateTime2(7) NOT NULL' and Type 'System.DateTime' in Column 'CreatedOn' of Type 'Event' may cause data loss when loading from the database.

The warning disappears if i change my column definition to datetime2(2) but 2 digits precision is lower than System.DateTime can handle, right? Why? How can i suppress the warning?


Solution

  • You might just ignore that warning. I've checked the source of sqlmetal (using Reflector) and found this:

    case SqlDbType.DateTime2:
    if (scale <= 2)
      return Compatibility.Compatible;
    else
      return Compatibility.DataLossFromDatabase;
    

    However, querying a datetime2 field from a sample database returned the whole 7 digit precision.