Recently I've encountered bugs due to DateTime precision errors in my SQL queries. I've narrowed the problem to the LINQ to SQL translation layer. While the relevant database columns are of type DateTime2, when I query the DataContext, the arguments are passed as a DbType DateTime parameter.
I believe this is caused by the default mapping of .Net DateTime to DbType DateTime in LINQ to SQL.
I am using attribute mapping and have double checked that affected columns have the column attribute DbType = "DATETIME2 NOT NULL".
Example:
Code: Select all
using (var dc = new DataContext(connectionString))
{
barEntity = (from bar in dc.Bars
select bar).FirstOrDefault();
}
using (var dc = new DataContext(connectionString))
{
var foo = (from bar in dc.Bars
where bar.Time == barEntity.Time
select bar).FirstOrDefault();
}
The only way I've found to force the parameter in the SQL query to be DbType DateTime2 is to either:
- Manually alter the parameter in the command (as seen here)
- Create and call a database function that has a parameter of type DateTime2
This seems like such a trivial issue as one would think the mapping could easily be changed to .Net DateTime -> DbType DateTime2 but I have yet to find a good way of overriding the default.
Any help is appreciated!