我正在使用 ASP.NET MVC 5/实体 6 框架和代码优先方法在 c# 中构建应用程序。我正在尝试将身份 2 中的 Id
属性从 string
更改为 integer
。
我关注了 documentation这似乎很简单。
然后我使用以下命令创建迁移
Add-Migration InitialCreate
由于某种原因,迁移文件中不包含 AspNet 表。但是,然后我使用以下命令进行了迁移
Update-Database
迁移已顺利执行,播种机运行良好。
但是,当我运行应用程序时
出现以下错误
Exception Details: System.FormatException: Input string was not in a correct format.
错误突出显示 Startup.Auth.cs
文件中的第 38 行。
Line 36: regenerateIdentityCallback: (manager, user) =>
Line 37: user.GenerateUserIdentityAsync(manager),
Line 38: getUserIdCallback: (id) => (id.GetUserId<int>()))
我该如何修复这个错误?
最佳答案
您需要删除您的 cookie。一个快速测试是使用不同的浏览器打开您的应用程序(还没有 cookie,可能使用 IE 或 Edge)。
这是因为当您第一次浏览该站点时,会创建一个 cookie(使用主键的字符串版本)。
当您将主键更改为 int 时,浏览器中的 cookie 仍在使用字符串版本,因此您将得到 Exception Details: System.FormatException: Input string was not in a correct format.
错误。
我曾经被这个错误困扰过一次,因此我记得我必须做些什么来修复它,这里有一个链接以获取更多信息:https://github.com/TypecastException/AspNet-Identity-2-With-Integer-Keys/issues/2
关于c# - 如何在 Identity 2 中将 Id 属性从字符串转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37716319/