2012-05-21 117 views
0

我有一個基於EF4模型的測試wcf webservice(REST)併發布在IIS7.5上。一個基本的查詢是wcf返回無效數據

public CfgUser GetUser(string fspId, string userName) 
    { 
    return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false); 
    } 

然後我做以下網址測試呼叫在IE9:

https://10.201.2.70/webservices/service.svc/rest/GetUser(EDS,kern) 

XML數據返回正確。 SQL事件探查器顯示

exec sp_executesql N'SELECT TOP (2) 
[Extent1].[UserId] AS [UserId], 
[Extent1].[FSP] AS [FSP], 
[Extent1].[UserGroupId] AS [UserGroupId], 
[Extent1].[ContactID] AS [ContactID], 
[Extent1].[UserName] AS [UserName], 
[Extent1].[UserPassword] AS [UserPassword], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[MidName] AS [MidName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[CommonName] AS [CommonName], 
[Extent1].[enumUserType] AS [enumUserType], 
[Extent1].[enumActive] AS [enumActive], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[wadtDeleted] AS [wadtDeleted], 
[Extent1].[wadtModifiedBy] AS [wadtModifiedBy], 
[Extent1].[wadtModifiedOn] AS [wadtModifiedOn], 
[Extent1].[wadtModifiedOnDTOffset] AS [wadtModifiedOnDTOffset], 
[Extent1].[wadtRowID] AS [wadtRowID], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[CreatedOnDTOffset] AS [CreatedOnDTOffset], 
[Extent1].[Email] AS [Email], 
[Extent1].[isDeviceUser] AS [isDeviceUser], 
[Extent1].[rowguid] AS [rowguid], 
[Extent1].[PasswordHash] AS [PasswordHash], 
[Extent1].[PasswordModifiedOn] AS [PasswordModifiedOn] 
FROM [dbo].[cfgUsers] AS [Extent1] 
WHERE (''EDS'' = [Extent1].[FSP]) AND ([Extent1].[UserName] = @p__linq__0) AND (0 = 
[Extent1].[wadtDeleted])',N'@p__linq__0 varchar(8000)',@p__linq__0='kern' 

如果我再做另一個直接調用與網址:

https://10.201.2.70/webservices/service.svc/rest/GetUser(recserv,recserv-admin) 

我收到一個請求錯誤。看來,EF linq查詢保留了傳遞給原始fspId參數(即「EDS」)的值。 SQL事件探查器顯示

exec sp_executesql N'SELECT TOP (2) 
[Extent1].[UserId] AS [UserId], 
[Extent1].[FSP] AS [FSP], 
[Extent1].[UserGroupId] AS [UserGroupId], 
[Extent1].[ContactID] AS [ContactID], 
[Extent1].[UserName] AS [UserName], 
[Extent1].[UserPassword] AS [UserPassword], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[MidName] AS [MidName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[CommonName] AS [CommonName], 
[Extent1].[enumUserType] AS [enumUserType], 
[Extent1].[enumActive] AS [enumActive], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[wadtDeleted] AS [wadtDeleted], 
[Extent1].[wadtModifiedBy] AS [wadtModifiedBy], 
[Extent1].[wadtModifiedOn] AS [wadtModifiedOn], 
[Extent1].[wadtModifiedOnDTOffset] AS [wadtModifiedOnDTOffset], 
[Extent1].[wadtRowID] AS [wadtRowID], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[CreatedOnDTOffset] AS [CreatedOnDTOffset], 
[Extent1].[Email] AS [Email], 
[Extent1].[isDeviceUser] AS [isDeviceUser], 
[Extent1].[rowguid] AS [rowguid], 
[Extent1].[PasswordHash] AS [PasswordHash], 
[Extent1].[PasswordModifiedOn] AS [PasswordModifiedOn] 
FROM [dbo].[cfgUsers] AS [Extent1] 
WHERE (''EDS'' = [Extent1].[FSP]) AND ([Extent1].[UserName] = @p__linq__0) AND (0 = 
[Extent1].[wadtDeleted])',N'@p__linq__0 varchar(8000)',@p__linq__0='recserv-admin' 

如果我重新啓動IIS服務,則第二個請求將工作,所以有明顯的領帶與IIS的地方? FSP和用戶之間有一對多關聯。

任何幫助/指針aprpeciated

+0

你可以發表你的界面如何看起來像? – Rajesh

回答

1
public CfgUser GetUser(string fspId, string userName) 
    { 
    return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false); 
    } 

如果這是你的應用程序真正的代碼示例,那麼你不應該有一個領域,因爲我看到治療上下文關係中每一個服務實例的創建實例被建造。

您應該更改上下文以使用using語句創建一個實例,以在您需要的數據返回後進行處理。這應該可以解決你的錯誤。

public CfgUser GetUser(string fspId, string userName) 
    { 
     using (MyEntities _context = new MyEntities()) 
     { 
    return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false); 
     } 
    } 
+0

這應該如何解決問題? 「舊」上下文的查詢不同於「新」上下文。 –

+0

實現Using語句取得了訣竅。 – Kern