我刚刚发布了一个关于adding properties to ApplicationUsers的问题,现在这个问题来自于:
现在,FirstName
的{{1}}和LastName
属性是AspNetUsers表中的字段。但是当我注册成为新用户时,我收到了这个错误:
System.Data.SqlClient.SqlException:无法将值NULL插入 列'LastName',表格 'ASPNET-CustomUserProps-20151019035309.dbo.AspNetUsers';专栏 不允许空值。 INSERT失败。
这是ApplicationUser
中的注册帖子:
AccountController
用户DO的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)
和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>
我还缺少什么?
这是堆栈跟踪的顶行(最后)几行,如果有帮助的话:
[SqlException(0x80131904):无法将值NULL插入列中 'LastName',表格 'ASPNET-CustomUserProps-20151019035309.dbo.AspNetUsers';专栏 不允许空值。 INSERT失败。声明已经终止。] System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 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,Boolean callerHasConnectionLock,Boolean asyncClose)+546
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔&amp; dataReady)+1693
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)+275
System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()+220
System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult的 asyncResult)+738
System.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult的 asyncResult)+147
答案 0 :(得分:1)
它必须成为将额外列添加到Identity db的方式。
您需要扩展IdentityUser
并重新定义DbContext
,并将IdentityDbContext
扩展为新的扩展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
请注意,这也允许您将db表名称更改为您选择的任何内容 - 因为您使用自己的DbContext覆盖。
答案 1 :(得分:0)
感谢A. Burak Erbora - 你的建议确实让我走上了正确的道路。但我并没有完全相同......它有点不同:
从头开始创建一个新项目,首先我创建了一个新的用户类和上下文,但是没有OnModelCreating函数:
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替换它)。
项目范围内是否将所有“ApplicationUser”替换为“AppUser”。
然后在项目范围内将所有“ApplicationDbContext”替换为“MyAppIdentityDbContext”。
确保在Web.config中,connectionString的名称与新上下文的名称相同(“IdentityConnection”)。
修改了RegisterViewModel,注册视图和帐户控制器以包含添加到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