系统。Linq2Sql中的DateTime和Sql datetime2(使用sqlmetal)

时间:2021-11-21 15:02:03

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数据类型来进行事件日志记录(作为标准的datetime,它的精度低于系统。但当我用sqlmetal生成代码时。我得到以下警告:

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: DbType“DateTime2(7) NOT NULL”和“Type”系统之间的映射。类型为“Event”的列“CreatedOn”中的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? Why? How can i suppress the warning?

如果我将列定义更改为datetime2(2),那么警告就会消失,但是2位数的精度低于系统。DateTime可以处理?为什么?我怎样才能不发出警告呢?

2 个解决方案

#1


4  

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位精度。

#2


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.

SQLMetal只是一个生成dbml文件的工具,因此它对运行时行为没有影响。但是作为一个生成工具,它可能不知道新的数据类型。

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

您是否尝试过使用XML编辑器自己编辑DBML以查看是否丢失了精确性?

#1


4  

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位精度。

#2


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.

SQLMetal只是一个生成dbml文件的工具,因此它对运行时行为没有影响。但是作为一个生成工具,它可能不知道新的数据类型。

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

您是否尝试过使用XML编辑器自己编辑DBML以查看是否丢失了精确性?