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

I would like to use the new Sql data type datetime2for event logging (since the standard datetime has lower accuracy than System.DateTimethat, which leads to data loss during storage), 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 the 'CreatedOn' column of the Event type can result in data loss when loading from the database.

The warning disappears if I change the definition of the column to datetime2(2), but the accuracy of 2 digits is lower than what System.DateTimecan be processed, right? What for? How can I suppress a warning?

+3
source share
2 answers

You can simply ignore this warning. I checked the sqlmetal source (using Reflector) and found this:

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

However, querying the datetime2 field from the sample database returned all 7-digit precision.

+4
source

SQLMetal is just a tool to create your dbml file so that it does not affect runtime behavior. But as a generation tool, he may not know about the new data type.

Have you tried editing DBML yourself with the XML editor to see if you lose accuracy?

+1
source

Source: https://habr.com/ru/post/1796221/


All Articles