我試圖得到一個MVC4網站使用SimpleMembership SQL工作,但每當我創建了一個用戶記錄我不斷收到錯誤:SimpleMembership提供商沒有分配ID的
[SqlException (0x80131904): Cannot insert the value NULL into column 'Id', table 'NFBC.dbo.User'; column does not allow nulls. INSERT fails.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753346
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295154
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +269
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1325
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +205
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +160
WebMatrix.Data.Database.Execute(String commandText, Object[] args) +116
WebMatrix.WebData.DatabaseWrapper.Execute(String commandText, Object[] parameters) +55
WebMatrix.WebData.SimpleMembershipProvider.CreateUserRow(IDatabase db, String userName, IDictionary`2 values) +1074
WebMatrix.WebData.SimpleMembershipProvider.CreateUserAndAccount(String userName, String password, Boolean requireConfirmation, IDictionary`2 values) +102
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(String userName, String password, Object propertyValues, Boolean requireConfirmationToken) +127
NFBC.Controllers.AccountController.Register(RegisterModel model) in e:\PROJECTS\DEVELOPMENT\00-Current\NFBC\NFBC\Controllers\AccountController.cs:74
所以對我來說這說,這是試圖向我的用戶表中插入一行,但它沒有設置「Id」屬性,這是表的主鍵。該ID是提供者所需的int列。下面的代碼是如何運行:
WebSecurity.InitializeDatabaseConnection("NFBCEntitiesRaw", "User", "Id", "Email1", autoCreateTables: true);
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
標識是一個int主鍵列,EMAIL1是username列,爲nvarchar(255)。
和連接字符串到數據庫:
<add name="NFBCEntitiesRaw" connectionString="data source=localhost;initial catalog=NFBC;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
我檢查WebSecurity.CreateUserAndAccount,SimpleMembershipProvider.CreateUserAndAccount和SimpleMembershipProvider.CreateUserRow的源代碼,並在任何這些方法沒有意義呢源代碼爲用戶記錄分配一個ID,這將確認拋出異常的原因。但是這個方法不允許我指定ID,並且當這個示例運行在SQLExpress上時,它似乎分配了ID很好......所以我不確定我在這裏做錯了什麼。
謝謝!
它只是發生在我身上,也許我應該設置數據庫自動分配該列與增量值,但我找不到任何地方在文檔中指定要做的是...是解決方案? –
這是連接到SQL Server實例嗎? –
是的,完整的SQL服務器實例。我想通了我需要使用身份功能。 –