对SQL/LINQ调试的帮助[英] Help with SQL/LINQ Debugging

本文是小编为大家收集整理的关于对SQL/LINQ调试的帮助的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在使用以下语句时遇到问题,它返回错误"序列不包含元素":

var vUser = (from u in this.dcLAUNCHOnline.aspnet_Users
                             where u.UserName.Equals(this.wCreateUser.UserName)
                             select u).Single();

正在生成的 SQL:

SELECT [t0].[ApplicationId],
 [t0].[UserId],
 [t0].[UserName],
 [t0].[LoweredUserName],
 [t0].[MobileAlias],
 [t0].[IsAnonymous],
 [t0].[LastActivityDate],
 [t0].[FirstName],
 [t0].[LastName],
 [t0].[Address_Street],
 [t0].[Address_City],
 [t0].[Address_Province],
 [t0].[Address_Country],
 [t0].[Address_PostalCode],
 [t0].[Telephone_Main_AreaCode],
 [t0].[Telephone_Main_Prefix],
 [t0].[Telephone_Main_LineNumber],
 [t0].[Telephone_Main_Extension],
 [t0].[Telephone_Mobile_AreaCode],
 [t0].[Telephone_Mobile_Prefix],
 [t0].[Telephone_Mobile_LineNumber],
 [t0].[Telephone_Mobile_Extension],
 [t0].[Telephone_Other_AreaCode],
 [t0].[Telephone_Other_Prefix],
 [t0].[Telephone_Other_LineNumber],
 [t0].[Telephone_Other_Extension],
 [t0].[Gender],
 [t0].[BirthDate]
FROM [dbo].[aspnet_Users] AS [t0]
WHERE [t0].[UserName] = @p0
-- @p0: Input NVarChar (Size = 20; Prec = 0; Scale = 0) [six.string@gmail.com]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918

SQL Server Management Studio 中运行时,脚本确实返回了我期望的行(在表中)

我用这一行定义了 p0:

DECLARE @p0 NVarChar(20) = 'six.string@gmail.com'

任何想法为什么会失败?谢谢!

推荐答案

只有当查询产生 no results 时,才会抛出你得到的 InvalidOperationException.

我唯一想到的(既然你说你确定数据库中存在该行),你可能正在连接到其他数据库强>.

检查您的 DataContext 的连接字符串,并确保您查询的是与 Management Studio 中相同的数据库.

编辑:顺便说一句,您正在直接查询 SqlMembershipProvider aspnet_Users 表,要通过 UserName 查找用户,您可能需要查看 Membership.FindUsersByName 方法.

本文地址:https://www.itbaoku.cn/post/1557221.html

问题描述

I'm having trouble with the following statement, which is returning the error "Sequence contains no elements":

var vUser = (from u in this.dcLAUNCHOnline.aspnet_Users
                             where u.UserName.Equals(this.wCreateUser.UserName)
                             select u).Single();

The SQL being generated:

SELECT [t0].[ApplicationId],
 [t0].[UserId],
 [t0].[UserName],
 [t0].[LoweredUserName],
 [t0].[MobileAlias],
 [t0].[IsAnonymous],
 [t0].[LastActivityDate],
 [t0].[FirstName],
 [t0].[LastName],
 [t0].[Address_Street],
 [t0].[Address_City],
 [t0].[Address_Province],
 [t0].[Address_Country],
 [t0].[Address_PostalCode],
 [t0].[Telephone_Main_AreaCode],
 [t0].[Telephone_Main_Prefix],
 [t0].[Telephone_Main_LineNumber],
 [t0].[Telephone_Main_Extension],
 [t0].[Telephone_Mobile_AreaCode],
 [t0].[Telephone_Mobile_Prefix],
 [t0].[Telephone_Mobile_LineNumber],
 [t0].[Telephone_Mobile_Extension],
 [t0].[Telephone_Other_AreaCode],
 [t0].[Telephone_Other_Prefix],
 [t0].[Telephone_Other_LineNumber],
 [t0].[Telephone_Other_Extension],
 [t0].[Gender],
 [t0].[BirthDate]
FROM [dbo].[aspnet_Users] AS [t0]
WHERE [t0].[UserName] = @p0
-- @p0: Input NVarChar (Size = 20; Prec = 0; Scale = 0) [six.string@gmail.com]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918

When run in SQL server management studio, the script does return the row I expect (which is in the table)

I defined p0 with this line:

DECLARE @p0 NVarChar(20) = 'six.string@gmail.com'

Any ideas why this is failing? Thanks!

推荐答案

The InvalidOperationException that you are getting is thrown only when the query yielded no results.

The only thing that comes to my mind, (since you say that you are sure that the row exists on the database), is that you are maybe connecting to other database.

Check your DataContext's connection string and make sure you are querying the same database as in Management Studio.

Edit: BTW, you are querying directly the SqlMembershipProvider aspnet_Users table, to find users by UserName, you might want to give a look to the Membership.FindUsersByName method.