2012-11-22 80 views
3

我試圖得到一個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很好......所以我不確定我在這裏做錯了什麼。

謝謝!

+0

它只是發生在我身上,也許我應該設置數據庫自動分配該列與增量值,但我找不到任何地方在文檔中指定要做的是...是解決方案? –

+0

這是連接到SQL Server實例嗎? –

+0

是的,完整的SQL服務器實例。我想通了我需要使用身份功能。 –

回答

5

假設您要連接到一個完整的MS SQL數據庫(如SQL Server 2008),如果需要自動增量,則需要將主鍵字段標記爲標識字段。您需要在設計模式下打開表格(假設您正在使用服務器管理工​​作室Visual Studio,請右鍵單擊表格並單擊「設計」)。選擇ID列,然後滾動Column Properties窗格,直到看到Identity Specification屬性。將IsIdentity更改爲true,保存並完成。它應該是這樣的:

enter image description here

HTH。

+0

是的,這是訣竅。在提交問題之後,我有點懷疑我需要這樣做,但SimpleMembership系統要求這樣做卻沒有提到它,這似乎很奇怪。好吧。我喜歡這個答案,但顯然我還沒有足夠的聲望呢。謝謝! –