asp.net mvc Core 网页错误提示:An unhandled exception occurred while processing the request.处理请求时发生未处理的异常。
网页错误提示:
An unhandled exception occurred while processing the request.
InvalidOperationException: The entity type 'IdentityUserLogin<string>' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
翻译:无效的操作异常:实体类型“IdentityUserLogin<string>”需要定义主键。如果您打算使用无钥匙实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无钥匙实体类型的更多信息,请参见https://go.microsoft.com/fwlink/?linkid=2141943.
解决办法:
打开项目的Data文件夹下的ApplicationDbContext.cs,添加如下方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//调用基类的OnModelCreating方法,设置数据库其它表和实体的映射关系
base.OnModelCreating(modelBuilder);
//InvalidOperationException: Unable to track an instance of type 'Article' because it does not have a primary key. Only entity types with a primary key may be tracked.
//无效的操作异常:无法跟踪类型为“Article”的实例,因为它没有主键。只能跟踪具有主键的实体类型。
//解决方法:如下,给实体类Article添加主键
modelBuilder.Entity<Article>().HasKey(c => new { c.ArticleId });
//同理,给实体类Column添加主键
modelBuilder.Entity<Column>().HasKey(c => new { c.ColumnId });
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构