开发者

Entity Framework object missing related data

开发者 https://www.devze.com 2023-01-31 10:14 出处:网络
I have an Entity Framework model that has two tables, client and postcode. Postcode can have many clients, client can have 1 postcode. They are joined on the postcode.

I have an Entity Framework model that has two tables, client and postcode. Postcode can have many clients, client can have 1 postcode. They are joined on the postcode. The two tables are mapped to views.

I have some clients that do not have a Postcode in the model, however in the DB they do!

I ran some tests and found postcodes that were returning clients when I do Postcode.Clients but not all of the clients? In the db a postcode had 14 related clients but EF was only returning the first 6. Basically certain postcodes are not returning all the data.

Lazy loading is turned on and I have tried turning it off without any luck.

Any ideas?

I am using VS 2010, C#, .NET 4.0, EF4 and SQL Server 2008

Thanks

UPDATE:

I have been running through this in LinqPad. I try the following code

Client c = Clients.Where(a => a.ClientId == 9063202).SingleOrDefault();
c.PostcodeView.Dump();

This returns null.

I then take the generated SQL and run this in a separate SQL query and it works correctly (after I add the @ to the start of the variable name)

SELECT TOP (2) 
[Extent1].[ClientId] AS [ClientId], 
[Extent1].[Surname] AS [Surname], 
[Extent1].[Forename] AS [Forename], 
[Extent1].[FlatNo] AS [FlatNo], 
[Extent1].[StNo] AS [StNo], 
[Extent1].[Street] AS [Street], 
[Extent1].[Town] AS [Town], 
[Extent1].[Postcode] AS [Postcode]
FROM (SELECT 
      [ClientView].[ClientId] AS [ClientId], 
      [ClientView].[Surname] AS [Surname], 
      [ClientView].[Forename] AS [Forename], 
      [ClientView].[FlatNo] AS [FlatNo], 
      [ClientView].[StNo] AS [StNo], 
      [ClientView].[Street] AS [Street], 
      [ClientView].[Town] AS [Town], 
      [ClientView].[Postcode] AS [Postcode]
      FROM [dbo].[ClientView] AS [ClientView]) AS [Extent1]
WHERE 9063202 = [Extent1].[ClientId]
GO

-- Region Parameters
DECLARE @EntityKeyValue1 VarChar(8) = 'G15 6NB'
-- EndRegion
SELECT 
[Extent1].[Postcode] AS [Postcode], 
[Extent1].[ltAstId] AS [ltAstId], 
[Extent1].[ltLhoId] AS [ltLhoId], 
[Extent1].[ltChcpId] AS [ltChcpId], 
[Extent1].[ltCppId] AS [ltCppId], 
[Extent1].[ltWardId] AS [ltWardId], 
[Extent1].[ltAst] AS [ltAst], 
[Extent1].[ltCpp] AS [ltCpp], 
[Extent1].[ltWard] AS [ltWard], 
[Extent1].[WardNo] AS [WardNo], 
[Extent1].[Councillor] AS [Councillor], 
[Extent1].[ltAdminCentre] AS [ltAdminCentre], 
[Extent1].[ltChcp] AS [ltChcp], 
[Extent1].[Forename] AS [Forename], 
[Extent1].[Surname] AS [Surname], 
[Extent1].[AreaNo] AS [AreaNo], 
[Extent1].[LtAomId] AS [LtAomId], 
[Extent1].[OOHltCoordinatorId] AS [OOHltCoordinatorId], 
[Extent1].[OvernightltCoordinatorId] AS [OvernightltCoordinatorId], 
[Extent1].[DayltCoordinatorId] AS [DayltCoordinatorId]
FROM (SELECT 
      [PostcodeView].[Postcode] AS [Postcode], 
      [PostcodeView].[ltAstId] AS [ltAstId], 
      [PostcodeView].[ltLhoId] AS [ltLhoId], 
      [PostcodeView].[ltChcpId] AS [ltChcpId], 
      [PostcodeView].[ltCppId] AS [ltCppId], 
      [PostcodeView].[ltWardId] AS [ltWardId], 
      [PostcodeView].[ltAst] AS [ltAst], 
      [PostcodeView].[ltCpp] AS [ltCpp], 
      [PostcodeView].[ltWard] AS [ltWard], 
      [PostcodeView].[WardNo] AS [WardNo], 
      [PostcodeView].[Councillor] AS [Councillor], 
      [PostcodeView].[ltAdminCentre] AS [ltAdminCentre], 
      [PostcodeView].[ltChcp] AS [ltChcp], 
      [PostcodeView].[Forename] AS [Forename], 
      [PostcodeView].[Surname] AS [Surname], 
      [PostcodeView].[AreaNo] AS [AreaNo], 
      [PostcodeView].[LtAomId] AS [LtAomId], 
      [PostcodeView].[DayltCoordinatorId] AS [DayltCoordinatorId], 
      [PostcodeView].[OOHltCoordinatorId] AS [OOHltCoordinato开发者_C百科rId], 
      [PostcodeView].[OvernightltCoordinatorId] AS [OvernightltCoordinatorId]
      FROM [dbo].[PostcodeView] AS [PostcodeView]) AS [Extent1]
WHERE [Extent1].[Postcode] = @EntityKeyValue1


Ended up removing the relationship and manually getting child data. Nasty but cannot find a reason why this is happening. Cheers for the comments

0

精彩评论

暂无评论...
验证码 换一张
取 消