I just recently posted a question regarding adding properties to ApplicationUsers, and now this question follows from that:
So now the FirstName
and LastName
properties for ApplicationUser
are fields in the AspNetUsers table. But when I go to register as a new user, I get this error:
System.Data.SqlClient.SqlException: Cannot insert the value NULL into
column 'LastName', table
'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers'; column does
not allow nulls. INSERT fails.
This is my Register Post in 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)
The FirstName
and LastName
properties of user DO contain the expected values, but this error occurs on the "Dim result =
..." line.
I added fields for First Name and Last Name in the Register View like so:
<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>
And to the Register View Model like so:
<Required>
<Display(Name:="First Name")>
Public Property FirstName As String
<Required>
<Display(Name:="Last Name")>
Public Property LastName As String
What else am I missing?
Here's the top (last) several lines of the stack trace, if that helps:
[SqlException (0x80131904): Cannot insert the value NULL into column
'LastName', table
'aspnet-CustomUserProps-20151019035309.dbo.AspNetUsers'; column does
not allow nulls. INSERT fails. The statement has been terminated.]
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, Boolean callerHasConnectionLock, 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,
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
See Question&Answers more detail:
os