TryUpdateModelAsync through exception when a value is null

时间:2018-03-25 19:12:45

标签: entity-framework asp.net-core-2.0 razor-pages

In the Add page I want to add all details of a staff member but, not all fields have to be added (The user might not want to enter his telephone).

if (await TryUpdateModelAsync<StaffMember>(
                    StaffMember, "StaffMember", 
                    s => s.FirstName, s => s.LastName,
                    s => s.Description, s => s.Telephone, s => s.RoomNumber,
                    s => s.SupervisionQuota, s => s.SecondMarkingQuota))
                {
                    StaffMember.UserId = StaffMemberUser.UserId;
                    _Context.StaffMember.Add(StaffMember);
                    await _Context.SaveChangesAsync();
                }

This is where I get the exception, When I don't enter a value for any of the fields I get

SqlException: String or binary data would be truncated.
The statement has been terminated.
DbUpdateException: An error occurred while updating the entries. See the inner exception for details.

Any idea of why is this happening

0 个答案:

没有答案