I have two tables Parent and Child. Both tables have a primary key “MyDate” declared as DateTime in Sql Server. Both tables have a record where MyDate is set to “2011-12-01 00:00:00.000”, i.e., 1st December 2011.
In my Linq to SQL dbml (.Net 3.5) I have an automatically created association between the two tables, on the MyDate property. The DataContext uses a login to the database that has its Default language set to “British English” in SQL Server (2008). When I run the Linq query:
var query
from parent in myDataContext.PARENTs
select parent
I get back Parent records but not the linked Child records.
In SQL Profiler I have this trace:
-- network protocol: TCP/IP
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language British
set dateformat dmy
set datefirst 1
set transaction isolation level read committed
exec sp_executesql N'SELECT [t0].[MyDate] FROM [dbo].[CHILD] AS [t0]
WHERE ([t0].[MYDATE] = @p0)','@p0 datetime',@p0='2011-12-01 00:00:00'
This returns 0 records; I have checked it by running it directly in a query window, using the same (British) login. If I comment out the “set dateformat dmy” I still get 0 records. If I run it using a login that is not British, I get a record back. If I use ’01-12-2011 00:00:00′ as the date (e.g., UK date format) and the British login, I get a record back.
It seems to me that Linq is not using the correct date format when it creates the date string to use in its query. As it is using “set dateformat dmy”, why is it not using that format in the date string it generates?
Is there a relatively simple way to fix this? Thanks.
Edit: in my model the MyDate property on both Parent and Child has a Server Data Type of “DateTime NOT NULL”, and a Type of “DateTime (System.DateTime)”.
Try eager loading
LINQ to SQL: Lazy and Eager Loading Hiccups
or
try out