3

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?

jaraics
  • 4,239
  • 3
  • 30
  • 35
UserControl
  • 14,766
  • 20
  • 100
  • 187

2 Answers2

4

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.

jaraics
  • 4,239
  • 3
  • 30
  • 35
1

SQLMetal is just a tool to generate your dbml file so it has no effect on runtime behaviour. But as a generation tool it may not be aware of the new datatype.

Have you tried editing the DBML yourself using an XML editor to see if you lose precision?

Christian Maslen
  • 1,100
  • 9
  • 13