0

我有一个基于 EF4 模型并在 IIS7.5 上发布的测试 wcf webservice (REST)。基本查询之一是

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

然后我在 ie9 中使用以下 url 进行测试调用:

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'

如果我再用 url 立即调用:

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 和用户之间存在一对多关联。

任何帮助/指针

4

1 回答 1

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);
        }
     }
于 2012-05-22T07:20:09.873 回答