【问题标题】:FormatException and ArgumentException errorsFormatException 和 ArgumentException 错误
【发布时间】:2019-04-08 11:15:15
【问题描述】:

我在ASP.NET Core 2.1 应用程序中的Visual Studio 中遇到这些错误,同时尝试将身份从字符串更改为长。有没有其他人遇到过这些错误?

处理请求时发生未处理的异常。 FormatException:输入字符串的格式不正确。 System.Number.StringToNumber(ReadOnlySpan str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal) ArgumentException: 81d1aa6c-b70a-4d64-a62a-e177d79b944e 不是 Int64 的有效值。参数名称:值 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext 上下文、CultureInfo 文化、对象值)

【问题讨论】:

  • 欢迎来到 Stack Overlow。您要转换的字符串中有什么值?您能否将正在执行转换(并导致引发异常)的代码添加到您的问题中。
  • 这里真的不足以真正帮助你。但是,我最好的猜测是您的数据库中有现有用户,并且在将字符串从字符串更改为长字符串后您没有费心进行迁移。结果,它现在尝试将旧的字符串 id 解析为 long,当然,失败了。
  • 我重新启动了Visual Studio(2017)并进行如下操作:删除迁移文件夹中的文件后删除数据库,添加迁移,更新数据库。现在,asp.net core 2.1 项目出现以下错误:处理请求时发生未处理的异常。 InvalidOperationException:没有注册类型“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”的服务。 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

标签: visual-studio asp.net-core argumentexception formatexception


【解决方案1】:

要将身份主键从 string 更改为 long,请按照以下步骤操作:

  • 添加ApplicationUser.cs

    public class ApplicationUser:IdentityUser<long>
    {
    }
    
  • 修改ApplicationDbContext

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }
    
  • 修改Startup.cs

            services.AddDefaultIdentity<ApplicationUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();
    
  • _LoginPartial.cshtmlIdentityUser修改为ApplicationUser

               @using Microsoft.AspNetCore.Identity
    
    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    
    @if (SignInManager.IsSignedIn(User))
    {
        <form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })" method="post" id="logoutForm" class="navbar-right">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
                </li>
                <li>
                    <button type="submit" class="btn btn-link navbar-btn navbar-link">Logout</button>
                </li>
            </ul>
        </form>
    }
    else
    {
        <ul class="nav navbar-nav navbar-right">
            <li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
            <li><a asp-area="Identity" asp-page="/Account/Login">Login</a></li>
        </ul>
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多