“[”附近的Npgsql错误42601语法错误

时间:2013-11-02 15:05:49

标签: npgsql

我在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: 

1 个答案:

答案 0 :(得分:0)

使用双引号“”代替'['和']'。在PostGreSql中,如果别名包含空格,则可以双引号。如果别名之间没有空格,则不需要双引号。