我正在 Udemy 中学习有关 ASP.NET MVC 的 Mosh Hamedani 课程之一。
我在使用代码优先(实体框架)设计数据库时遇到了一个错误。
起初,我收到了“在程序集中找不到 DbContext”的错误消息。解决了这个问题后,又一个突然冒出来了。
下图将显示添加迁移时发现的错误。我已经搜索了相同的错误,但没有找到。我在过去的两个小时里一直在挣扎,但到目前为止还没有解决任何问题。
无法创建“Vidly_Context”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728
使用 (2) 参数添加自己的 DbContext 构造函数后出现类似问题。应用程序正常,但迁移停止工作。通过使用来自Dotnet 工具 @xspdf 的信息并通过方法 + 硬编码默认连接字符串(如果未设置)替换提到的构造函数,通过第一次更新 EF(使用 5 时出于奇怪原因使用 3.1.5)来修复。
dotnet tool update --global dotnet-ef
// following command show the most during migration build/run in cmd
// mind current dir is Migrations folder of (VS) startup project here
dotnet ef --startup-project ../ --verbose migrations add test
Run Code Online (Sandbox Code Playgroud)
3.1.5 & 上下文激活错误
The Entity Framework …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc entity-framework entity-framework-core entity-framework-migrations