我對SQLite數據庫有Entity Framework 4模型。部分模型看起來像這樣 LINQ to Entities物化錯誤
所有Id字段的類型都是Guid(SQLite表DDL中的uniqueidentifier)。但是當我運行下面的LINQ查詢時,我得到異常。
var query = AbonentPfrs.Select(a =>
new
{
AbPfr = a,
Pfr = a.PfrCertificates.Select(c => c.Id),
Ac = a.AcCertificates.Select(c => c.Id)
}
);
query.ToList();
當我運行它,我得到
System.InvalidOperationException: The type of the key field 'Id' is expected to be 'System.Guid', but the value provided is actually of type 'System.String'.
更新:我發現EF生成SQL查詢,真正返回一個字符串作爲實體的一個標識符。
行進至DB ((query as ObjectQuery).ToTraceString())
的SQL是這樣的:
SELECT
[UnionAll1].[C2] AS [C1],
[UnionAll1].[C3] AS [C2],
[UnionAll1].[Id] AS [C3],
[UnionAll1].[PfrRegNumber] AS [C4],
[UnionAll1].[PfrUnitId] AS [C5],
[UnionAll1].[Account_Id] AS [C6],
[UnionAll1].[ActiveCertificate_Id] AS [C7],
[UnionAll1].[C1] AS [C8],
[UnionAll1].[Id1] AS [C9],
[UnionAll1].[C4] AS [C10]
FROM (SELECT
CASE WHEN ([Extent2].[Id] IS NULL) THEN NULL ELSE 1 END AS [C1],
1 AS [C2],
1 AS [C3],
[Extent1].[Id] AS [Id],
[Extent1].[PfrRegNumber] AS [PfrRegNumber],
[Extent1].[PfrUnitId] AS [PfrUnitId],
[Extent1].[Account_Id] AS [Account_Id],
[Extent1].[ActiveCertificate_Id] AS [ActiveCertificate_Id],
[Extent2].[Id] AS [Id1],
NULL AS [C4]
FROM [AbonentPfrs] AS [Extent1]
LEFT OUTER JOIN [Certificates] AS [Extent2] ON [Extent1].[Id] = [Extent2].[AbonentPfr_PfrCertificates_Certificate_Id]
UNION ALL
SELECT
2 AS [C1],
2 AS [C2],
2 AS [C3],
[Extent3].[Id] AS [Id],
[Extent3].[PfrRegNumber] AS [PfrRegNumber],
[Extent3].[PfrUnitId] AS [PfrUnitId],
[Extent3].[Account_Id] AS [Account_Id],
[Extent3].[ActiveCertificate_Id] AS [ActiveCertificate_Id],
NULL AS [C4],
[Extent4].[Id] AS [Id1]
FROM [AbonentPfrs] AS [Extent3]
INNER JOIN [Certificates] AS [Extent4] ON [Extent3].[Id] = [Extent4].[AbonentPfr_AcCertificates_Certificate_Id]) AS [UnionAll1]
ORDER BY [UnionAll1].[Id] ASC, [UnionAll1].[C1] ASC
的最後列的證書中的實體ID。但最後有Guid類型和之前的字符串。如果我單獨運行查詢的部分,然後我得到的下一列類型(FOT最後兩個):
- 首先選擇:GUID,對象
- 第二個選擇:對象,的Guid
- UNION ALL:GUID,對象
- 外選擇:字符串的Guid
爲什麼在倒數第二個欄外select返回字符串?
我認爲這是因爲兩個關係在同一個表上 - 從AbonentPfr到證書。但我不確定... – sedovav
可能是因爲SQLite存儲GUID作爲字符串? AFAIK SQLite沒有本地GUID數據類型。 – okrumnow
但在所有其他查詢中,它的工作原理相同 – sedovav