'Linq to Sql isn't pulling from identity cache
I'm facing a strange problem with L2S identity cache. In my PoC I've made a simple data context with only one table but if I make the same query the second one doesn't get pulled by the identity cache, so L2Q does double DB access somewhat I want to avoid, here is the SQL code:
CREATE TABLE [dbo].[Prodotto](
[c_compagnia] [char](1) NOT NULL,
[c_prodotto] [char](5) NOT NULL,
[descrizione] [varchar](50) NOT NULL,
CONSTRAINT [PK_Prodotto] PRIMARY KEY CLUSTERED
(
[c_compagnia] ASC,
[c_prodotto] ASC
)
and here .Net code:
DataClasses2DataContext context = new DataClasses2DataContext("Data Source=someone;Initial Catalog=test;Integrated Security=True");
context.Log = Console.Out;
var x = context.Prodottos.
Where(c => c.c_compagnia.Equals('U') && c.c_prodotto.Equals("MU ")).
First();
Console.WriteLine(x);
var xx = context.Prodottos.
Where(c => c.c_compagnia.Equals('U') && c.c_prodotto.Equals("MU ")).
First();
Console.WriteLine(Object.ReferenceEquals(x,xx));
these are the redundant query sent to the DB:
SELECT TOP (1) [t0].[c_compagnia], [t0].[c_prodotto], [t0].[descrizione]
FROM [dbo].[Prodotto] AS [t0]
WHERE ([t0].[c_compagnia] = @p0) AND ([t0].[c_prodotto] = @p1)
-- @p0: Input Char (Size = 1; Prec = 0; Scale = 0) [U]
-- @p1: Input VarChar (Size = 5; Prec = 0; Scale = 0) [MU ]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.9141
SELECT TOP (1) [t0].[c_compagnia], [t0].[c_prodotto], [t0].[descrizione]
FROM [dbo].[Prodotto] AS [t0]
WHERE ([t0].[c_compagnia] = @p0) AND ([t0].[c_prodotto] = @p1)
-- @p0: Input Char (Size = 1; Prec = 0; Scale = 0) [U]
-- @p1: Input VarChar (Size = 5; Prec = 0; Scale = 0) [MU ]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.9141
What am I doing wrong? Any help would be appreciated, thanks a lot
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
