2

我在 InitializeDataBaseConnection() 上遇到了这个异常。异常是 Npgsql.NpgsqlException 未被用户代码处理。生成的 SQL 似乎对表名和列名有方括号,而 Npgsql 不喜欢它。我怎样才能绕过这个错误?

这是我的 SimpleMembershipProvider 构造函数:

public SimpleMembershipInitializer()
{
Database.SetInitializer<UsersContext>(null);

try
{
    using (var context = new UsersContext())
    {
        if (!context.Database.Exists())
        {
            // Create the SimpleMembership database without Entity Framework migration schema
            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
        }
    }

    WebSecurity.InitializeDatabaseConnection("MyDBContext", "UserProfile", "UserId", "UserName", autoCreateTables: false);
}
catch (Exception ex)
{
     throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}


  **Message=ERROR: 42601: syntax error at or near "["**
  Source=Npgsql
  ErrorCode=-2147467259
  BaseMessage=syntax error at or near "["

  Code=42601
  Detail=""
  **ErrorSql=SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = ((E'Z')))**
  File=src\backend\parser\scan.l
  Hint=""
  Line=1044
  Position=8
  Routine=scanner_yyerror
  Severity=ERROR
  Where=""
  StackTrace:
       at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext() in C:\projects\Npgsql2\src\Npgsql\NpgsqlState.cs:line 850
       at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject() in C:\projects\Npgsql2\src\Npgsql\NpgsqlDataReader.cs:line 1173
       at Npgsql.ForwardsOnlyDataReader.GetNextRowDescription() in C:\projects\Npgsql2\src\Npgsql\NpgsqlDataReader.cs:line 1191
       at Npgsql.ForwardsOnlyDataReader.NextResult() in C:\projects\Npgsql2\src\Npgsql\NpgsqlDataReader.cs:line 1377
       at Npgsql.ForwardsOnlyDataReader..ctor(IEnumerable`1 dataEnumeration, CommandBehavior behavior, NpgsqlCommand command, NotificationThreadBlock threadBlock, Boolean synchOnReadError) in C:\projects\Npgsql2\src\Npgsql\NpgsqlDataReader.cs:line 1040
       at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb) in C:\projects\Npgsql2\src\Npgsql\NpgsqlCommand.cs:line 611
       at Npgsql.NpgsqlCommand.ExecuteScalar() in C:\projects\Npgsql2\src\Npgsql\NpgsqlCommand.cs:line 722
       at WebMatrix.Data.Database.QueryValue(String commandText, Object[] args)
       at WebMatrix.WebData.DatabaseWrapper.QueryValue(String commandText, Object[] parameters)
       at WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase db, String userTableName, String userNameColumn, String userIdColumn, String userName)
       at WebMatrix.WebData.SimpleMembershipProvider.ValidateUserTable()
  InnerException: 
4

1 回答 1

0

使用双引号 '"' 而不是 '[' 和 ']'。在 PostGreSql 中,如果别名包含空格,您可以将它们双引号。如果别名之间没有空格,则不需要双引号。

于 2014-08-07T05:17:46.443 回答