简体   繁体   English

Linq2Sql中的System.DateTime和Sql datetime2(使用sqlmetal)

[英]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: 我想利用新的Sql datetime2数据类型进行事件记录(因为标准日期时间的精度低于System.DateTime导致存储时数据丢失)但是当我使用sqlmetal.exe生成代码时,我收到以下警告:

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. db.dbml(98):警告DBML1008:类型'事件'的列'CreatedOn'中的DbType'DateTime2(7)NOT NULL'和类型'System.DateTime'之间的映射可能导致从数据库加载时数据丢失。

The warning disappears if i change my column definition to datetime2(2) but 2 digits precision is lower than System.DateTime can handle, right? 如果我将列定义更改为datetime2(2)但2位数的精度低于System.DateTime可以处理,警告消失,对吗? Why? 为什么? How can i suppress the warning? 我怎么能抑制警告?

You might just ignore that warning. 您可能只是忽略该警告。 I've checked the source of sqlmetal (using Reflector) and found this: 我检查了sqlmetal的源代码(使用Reflector),发现了这个:

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. 但是,从示例数据库查询datetime2字段会返回整个7位精度。

SQLMetal is just a tool to generate your dbml file so it has no effect on runtime behaviour. SQLMetal只是一个生成dbml文件的工具,因此它对运行时行为没有影响。 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? 您是否尝试使用XML编辑器自行编辑DBML以查看是否丢失了精度?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM