gpt4 book ai didi

c# - SimpleMembership Provider 不分配 ID

转载 作者:行者123 更新时间:2023-11-30 15:01:52 29 4
gpt4 key购买 nike

我正在尝试通过使用 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 就好了......所以我不太确定我在这里做错了什么。

谢谢!

最佳答案

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

enter image description here

HTH.

关于c# - SimpleMembership Provider 不分配 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505011/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com