SimpleMembership Provider不分配ID

时间:2012-11-22 02:17:21

标签: c# .net asp.net-mvc-4 simplemembership

我正在尝试使用SimpleMembership获取一个使用SQL的MVC4站点,但每当我创建用户记录时我都会遇到错误:

[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);

Id是一个int主键列,Email1是用户名列,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 ......所以我不确定我在这里做错了什么。

谢谢!

1 个答案:

答案 0 :(得分:5)

假设您要连接到成熟的MS SQL数据库(如SQL Server 2008),如果要自动增加,则需要将主键字段标记为标识字段。您需要在设计模式下打开表格(假设您使用 Server Management Studio Visual Studio ,右键单击表格并单击“设计”)。选择ID列,然后滚动Column Properties窗格,直到您看到Identity Specification属性。将IsIdentity更改为true,保存,然后就完成了。它应该是这样的:

enter image description here

HTH。