UserControl
UserControl

Reputation: 15159

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?

Upvotes: 3

Views: 1311

Answers (2)

jaraics
jaraics

Reputation: 4299

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.

Upvotes: 4

Christian Maslen
Christian Maslen

Reputation: 1110

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?

Upvotes: 1

Related Questions