我最近发布了一个关于向 ApplicationUsers 添加属性的问题,现在这个问题由此而来:
因此,现在ApplicationUser
的FirstName
和LastName
属性是 AspNetUsers 表中的字段。 但是当我注册为新用户时,出现此错误:
System.Data.SqlClient.SqlException:无法将值 NULL 插入 列"姓氏",表 'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers';列执行 不允许空值。插入失败。
这是我在AccountController
的注册帖子:
Public Async Function Register(model As RegisterViewModel) As Task(Of ActionResult)
If ModelState.IsValid Then
Dim user = New ApplicationUser() With {
.UserName = model.Email,
.Email = model.Email,
.FirstName = model.FirstName,
.LastName = model.LastName
}
Dim result = Await UserManager.CreateAsync(user, model.Password)
用户 DO 的 FirstName
和 LastName
属性包含预期值,但此错误发生在"Dim result =
..."线。
我在注册视图中添加了名字和姓氏字段,如下所示:
<div class="form-group">
@Html.LabelFor(Function(m) m.Email, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(Function(m) m.Email, New With {.class = "form-control"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(Function(m) m.FirstName, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(Function(m) m.FirstName, New With {.class = "form-control"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(Function(m) m.LastName, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(Function(m) m.LastName, New With {.class = "form-control"})
</div>
</div>
对于寄存器视图模型,如下所示:
<Required>
<Display(Name:="First Name")>
Public Property FirstName As String
<Required>
<Display(Name:="Last Name")>
Public Property LastName As String
我还错过了什么?
这是堆栈跟踪的前(最后(几行,如果有帮助的话:
[SqlException (0x80131904(: 无法将值 NULL 插入到列中 "姓氏",表 'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers';列执行 不允许空值。插入失败。该声明已终止。
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action'1 wrapCloseInAction( +1787814
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action'1 wrapCloseInAction( +5341674 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, 布尔调用方HasConnectionLock, Boolean asyncClose( +546
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady( +1693
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, 运行行为 运行行为,字符串重置选项字符串( +275
System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader(( +220
System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult 异步结果( +738
System.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult 异步结果( +147
它必须是将额外列添加到标识数据库的方式。
您需要扩展IdentityUser
并通过从 IdentityDbContext
扩展来重新优化DbContext
,并将新的扩展IdentityUser
类作为其泛型参数。以下是您的操作方法:
Public Class YourApplicationUser
Inherits IdentityUser
Public Property LastName() As String
Get
Return m_LastName
End Get
Set
m_LastName = Value
End Set
End Property
Private m_LastName As String
Public Function GenerateUserIdentityAsync(manager As UserManager(Of YourApplicationUser)) As Task(Of ClaimsIdentity)
' Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
Dim userIdentity = Await manager.CreateIdentityAsync(Me, DefaultAuthenticationTypes.ApplicationCookie)
' Add custom user claims here
Return userIdentity
End Function
End Class
Public Class YourApplicationIdentityDbContext
Inherits IdentityDbContext(Of YourApplicationUser)
Public Sub New()
MyBase.New("IdentityConnection", throwIfV1Schema := False)
End Sub
Public Shared Function Create() As YourApplicationIdentityDbContext
Return New YourApplicationIdentityDbContext()
End Function
Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of YourApplicationIdentityDbContext)().ToTable("IdentityUser").[Property](Function(p) p.Id).HasColumnName("UserId")
modelBuilder.Entity(Of IdentityUserRole)().ToTable("IdentityUserRole").HasKey(Function(p) New From { _
p.RoleId, _
p.UserId _
})
modelBuilder.Entity(Of IdentityUserLogin)().ToTable("IdentityUserLogin").HasKey(Function(p) New From { _
p.LoginProvider, _
p.ProviderKey, _
p.UserId _
})
modelBuilder.Entity(Of IdentityUserClaim)().ToTable("IdentityUserClaim").HasKey(Function(p) p.Id).[Property](Function(p) p.Id).HasColumnName("UserClaimId")
modelBuilder.Entity(Of IdentityRole)().ToTable("IdentityRole").[Property](Function(p) p.Id).HasColumnName("RoleId")
End Sub
End Class
请注意,这也允许您将数据库表名称更改为您选择的任何名称 - 因为您可以使用自己的 DbContext 进行覆盖。
感谢 A. Burak Erbora - 你的建议确实让我走上了正确的道路。 但我做的并不完全相同...它有点不同:
从头开始一个新项目,首先我创建了一个新的用户类和上下文,但没有 OnModelCreate 函数:
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Threading.Tasks
Imports System.Security.Claims
Imports Microsoft.AspNet.Identity
Public Class AppUser
Inherits IdentityUser
Private m_FirstName As String
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(value As String)
m_FirstName = value
End Set
End Property
Private m_LastName As String
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(value As String)
m_LastName = value
End Set
End Property
Public Async Function GenerateUserIdentityAsync(manager As UserManager(Of AppUser)) As Task(Of ClaimsIdentity)
' Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
Dim userIdentity = Await manager.CreateIdentityAsync(Me, DefaultAuthenticationTypes.ApplicationCookie)
' Add custom user claims here
Return userIdentity
End Function
End Class
Public Class MyAppIdentityDbContext
Inherits IdentityDbContext(Of AppUser)
Public Sub New()
MyBase.New("IdentityConnection", throwIfV1Schema:=False)
End Sub
Public Shared Function Create() As MyAppIdentityDbContext
Return New MyAppIdentityDbContext()
End Function
End Class
然后我删除了IdentityModels(AppUser替换了它(。
是否在项目范围内将所有"应用程序用户"替换为"应用程序用户"。
然后,项目范围将所有"ApplicationDbContext"替换为"MyAppIdentityDbContext"。
确保在 Web.config 中 connectionString 的名称与新上下文的名称相同("IdentityConnection"(。
修改了注册视图模型、注册视图和帐户控制器,以包括添加到 AppUser 的字段。
从那里开始,所有用户的东西都是一样的。 例如,我想在用户登录时在导航栏中显示名字而不是电子邮件地址,因此这是_LoginPartial视图:
@Imports Microsoft.AspNet.Identity
@Code
Dim db = New MyAppIdentityDbContext
Dim curUserID = User.Identity.GetUserId()
Dim myFirstName As String = (From users In db.Users Where users.Id = curUserID Select users.FirstName).FirstOrDefault
End Code
@If Request.IsAuthenticated
@Using Html.BeginForm("LogOff", "Account", FormMethod.Post, New With { .id = "logoutForm", .class = "navbar-right" })
@Html.AntiForgeryToken()
@<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + myFirstName + "!", "Index", "Manage", routeValues:=Nothing, htmlAttributes:=New With {.title = "Manage"})
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
End Using
Else
@<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Account", routeValues := Nothing, htmlAttributes := New With { .id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues := Nothing, htmlAttributes := New With { .id = "loginLink" })</li>
</ul>
End If