C# 添加服务器端验证在获取用户列表时导致异常
Asp.net核心3.1 我已经扩展了IdentityUser并创建了ApplicationUserC# 添加服务器端验证在获取用户列表时导致异常,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,Asp.net核心3.1 我已经扩展了IdentityUser并创建了ApplicationUser public class ApplicationUser : IdentityUser { [MaxLength ( 150 ) ] public string FirstName { get ; set ; } [ MaxLength ( 150 ) ] public string LastName { get ; set ; } public in
public class ApplicationUser : IdentityUser
{
[MaxLength ( 150 ) ]
public string FirstName { get ; set ; }
[ MaxLength ( 150 ) ]
public string LastName { get ; set ; }
public int AlternateUserId { get ; set ; }
[ MaxLength ( 150 ) ]
public string CompanyName { get ; set ; }
[ MaxLength ( 38 ) ]
[ Required ]
public string ClientId { get ; set ; }
[ Required ]
public int ShortClient { set ; get ; }
public bool Locked { set ; get ; }
public bool Admin { set ; get ; }
}
我创建了一个迁移,更新了创建所有用户的数据库。。。。。。桌子
然后,我构建了数据库以获得crud控制器和视图
这给了我这部分课程
public partial class Aspnetusers
{
public Aspnetusers()
{
Aspnetuserclaims = new HashSet<Aspnetuserclaims>();
Aspnetuserlogins = new HashSet<Aspnetuserlogins>();
Aspnetuserroles = new HashSet<Aspnetuserroles>();
Aspnetusertokens = new HashSet<Aspnetusertokens>();
Usertoroles = new HashSet<Usertoroles>();
}
[Key]
public string Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
[Required(ErrorMessage = "Please enter an email.")]
[EmailAddress(ErrorMessage = "Please enter a valid email address.")]
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string ConcurrencyStamp { get; set; }
// [Required(ErrorMessage = "Please enter an email.")]
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEnd { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
// [Required(ErrorMessage = "Please enter the first name.")]
public string FirstName { get; set; }
// [Required(ErrorMessage = "Please enter the last name.")]
public string LastName { get; set; }
public int AlternateUserId { get; set; }
public string CompanyName { get; set; }
// [Required(ErrorMessage = "You must choose a company")]
public string ClientId { get; set; }
public int ShortClient { get; set; }
public bool Locked { get; set; }
public bool Admin { set; get; }
public virtual Company ShortClientNavigation { get; set; }
public virtual ICollection<Aspnetuserclaims> Aspnetuserclaims { get; set; }
public virtual ICollection<Aspnetuserlogins> Aspnetuserlogins { get; set; }
public virtual ICollection<Aspnetuserroles> Aspnetuserroles { get; set; }
public virtual ICollection<Aspnetusertokens> Aspnetusertokens { get; set; }
public virtual ICollection<Usertoroles> Usertoroles { get; set; }
}
如果我调试到MS代码中,我发现了异常
“((System.Linq.IQueryable)集合).ElementType”引发了异常
类型为“System.NotImplementedException”
记录器返回异常
System.InvalidCastException:无法强制转换类型为的对象
“System.DBNull”以键入“System.String”。在
中的MySqlConnector.Core.Row.GetString(Int32序号)
C:\projects\mysqlconnector\src\mysqlconnector\Core\Row.cs:第344行在中的MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32序号)处 C:\projects\mysqlconnector\src\mysqlconnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 272在lambda_方法(闭包、QueryContext、DbDataReader、, ResultContext,Int32[],ResultCoordinator)位于 Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.MoveNext()
位于System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
在System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)处
SextantCoreHostApp.Areas.Users.Controllers.UsersController.UserIndex()
在里面
C:\Users\Colin\Documents\Development\SextantCore3WithPermissions\sextantcorhostapp\Areas\Users\Controllers\userscocontroller.cs:line
35分
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper
映射器,ObjectMethodExecutor,对象控制器,对象[]
(a)在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|u waiting | 12_0(ControllerActionInvoker
调用程序,ValueTask`1 actionResultValueTask)位于
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|u waiting|10_0(ControllerActionInvoker
调用程序、任务lastTask、状态next、作用域、对象状态、布尔值
完成)在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed
(上下文)在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态)&
接下来,在
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
---来自引发异常的上一个位置的堆栈结束跟踪---在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u waiting | 24_0(ResourceInvoker
调用程序、任务lastTask、状态next、作用域、对象状态、布尔值
完成)在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed
(上下文)在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(状态&
接下来,在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
---来自引发异常的上一个位置的堆栈结束跟踪---在
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u Logged|17|u 1(ResourceInvoker
调用程序)在
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext
httpContext)在
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g|u等待| 6|0(ExceptionHandlerMiddleware
中间件、HttpContext上下文、任务)位于
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext
上下文,例外DispatchInfo edi)位于
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g|u等待| 6|0(ExceptionHandlerMiddleware
中间件、HttpContext上下文、任务)位于
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext
(上下文)在
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext
(上下文)在
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext
(上下文)在
Serilog.AspNetCore.RequestLoggingMiddleware.Invoke(HttpContext
httpContext)在
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g|u等待| 6|0(ExceptionHandlerMiddleware
中间件、HttpContext上下文、任务
这里缺少什么?用户表中的一个字段返回空值(System.DBNull),因此无法将其分配给强制字符串属性 标记为必需的字段不为空。表中只有一行数据。我再次检查,该行中的一个字段为空。这就解决了问题。谢谢
public class UsersController : Controller
{
private readonly PCSLoginContext context;
private ILogger<CompaniesController> logger;
public UsersController(PCSLoginContext context, ILogger<CompaniesController> logger)
{
this.context = context;
this.logger = logger ;
}
// GET: Aspnetusers
public async Task<IActionResult> UserIndex()
{
var users = _context.Aspnetusers.ToList ( ) ;
return View( users);
}
}
var users = _context.Aspnetusers.ToList ( ) ;