如何解决vs 2019 社区版中 .net core 5.0 的 .net core ef 迁移难题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计458个文字,预计阅读时间需要2分钟。
问题一:执行Add-Migration时出现的问题内容:当执行Add-Migration时,遇到模型类主键标识问题。通常都是模型类缺少主键标识问题,增加KEY即可解决。错误信息:The entity type 'xxxx' requires a primary key to be defined. If you intended to use a keyless entity type, call 'Ha'
解决方法:确保模型类中定义了主键。如果未定义,添加主键即可。
问题一:Add-Migration 时出现 此类问题一般都是模型类主键标识问题增加KEY即可解决
报错:
The entity type 'xxxx' 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 go.microsoft.com/fwlink/?linkid=2141943.
以上问题方案:
public class XXXX { **[Key]** public int UserID { get; set; }//用户识别ID 主键 public string UserNike { get; set; } }
问题二:
Add-Migration 时出现这类问题(我是因为我选了默认项目模型类)) 一般情况下是当前默认项目(可以切到需要生成的项目下)中缺少entity framework core 相关包,需引用包
报错:
Your target project '解决方案名' doesn't match your migrations assembly '默认项目'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("WebBlogFirst")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.以上问题方案:
Add-Migration 时出现这类问题(我是因为我选了默认项目模型类)) 一般情况下是当前默认项目(可以切到需要生成的项目下)中缺少entity framework core 相关包,需引用包**
本文共计458个文字,预计阅读时间需要2分钟。
问题一:执行Add-Migration时出现的问题内容:当执行Add-Migration时,遇到模型类主键标识问题。通常都是模型类缺少主键标识问题,增加KEY即可解决。错误信息:The entity type 'xxxx' requires a primary key to be defined. If you intended to use a keyless entity type, call 'Ha'
解决方法:确保模型类中定义了主键。如果未定义,添加主键即可。
问题一:Add-Migration 时出现 此类问题一般都是模型类主键标识问题增加KEY即可解决
报错:
The entity type 'xxxx' 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 go.microsoft.com/fwlink/?linkid=2141943.
以上问题方案:
public class XXXX { **[Key]** public int UserID { get; set; }//用户识别ID 主键 public string UserNike { get; set; } }
问题二:
Add-Migration 时出现这类问题(我是因为我选了默认项目模型类)) 一般情况下是当前默认项目(可以切到需要生成的项目下)中缺少entity framework core 相关包,需引用包
报错:
Your target project '解决方案名' doesn't match your migrations assembly '默认项目'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("WebBlogFirst")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.以上问题方案:

