简体   繁体   English

Add-Migration 命令在我的 .NET 6 项目中有错误

[英]Add-Migration command has an error in my .NET 6 project

I have a project and created it using .NET 6.我有一个项目并使用 .NET 6 创建它。

I used the Add-Migration command before adding some DbSet in my DbContext , it worked correctly and made migration files for me.在我的DbContext添加一些DbSet之前,我使用了Add-Migration命令,它工作正常并为我制作了迁移文件。

Today add a new DbSet to my DbContext after that used the Add-Migration command but I get an error:今天使用Add-Migration命令向我的DbContext添加一个新的DbSet ,但出现错误:

Unable to create an object of type 'HospitalNetDbContext'.无法创建“HospitalNetDbContext”类型的 object。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

I get some information from this link but I can't solve my problem.我从这个链接获得了一些信息,但我无法解决我的问题。

Some people suggested using contractor without parameter in DbContext class, but when I add this contractor to DbContext class and after that, I used the Add-Migration command, I get a new error:有人建议在DbContext class 中使用没有参数的承包商,但是当我将此承包商添加到DbContext class 之后,我使用了Add-Migration命令,我收到了一个新错误:

No database provider has been configured for this DbContext.没有为此 DbContext 配置数据库提供程序。 A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider.可以通过覆盖“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。 If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.如果使用了“AddDbContext”,那么还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions object 并将其传递给 DbContext 的基本构造函数。

After this error I added a new constructor same as below but my problem still persists:在此错误之后,我添加了一个与以下相同的新构造函数,但我的问题仍然存在:

public HospitalNetDbContext(DbContextOptions<HospitalNetDbContext> options) : base(options)
{
}

More information about my project is connection string defined into the app settings file and in startup file using below code for set connection string to DbContext instead of add into overriding OnConfiguring method in the DbContext class.关于我的项目的更多信息是定义到应用程序设置文件和启动文件中的连接字符串,使用下面的代码将连接字符串设置为DbContext ,而不是添加到DbContext class 中的覆盖OnConfiguring方法中。

services.AddDbContext<HospitalNetDbContext>(x => x.UseSqlServer(Configuration.GetConnectionString("HospitalNetDb")));

I had the same error and the issue for me was that I hadn't specified the startup project and target project.我有同样的错误,对我来说问题是我没有指定启动项目和目标项目。

The class I was using that inherited from DbContext was in a data access project separate from the startup project, and I think it meant that without specifying the startup project the configuration in program.cs hadn't run when it was trying to initialise the context class and that's why it failed.我使用的 class 继承自 DbContext 位于与启动项目分开的数据访问项目中,我认为这意味着如果没有指定启动项目,program.cs 中的配置在尝试初始化上下文时没有运行class 这就是它失败的原因。 When I ran it like this it worked:当我这样运行它时,它起作用了:

Add-Migration -Name MyNewMigration -StartupProject {startup project name} -Project {project containing HospitalNetDbContext}

This page was helpful for me https://docs.microsoft.com/en-us/ef/core/cli/powershell#target-and-startup-project .这个页面对我很有帮助https://docs.microsoft.com/en-us/ef/core/cli/powershell#target-and-startup-project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么命令“ add-migration”不接受我在project.json中安装的软件包? - Why is the command 'add-migration' not accepting my installed packages in project.json? ASP.NET Core EF Add-Migration 命令不起作用 - ASP.NET Core EF Add-Migration command not working ReflectionTypeLoadException在程序包管理器控制台中运行add-migration命令时出错 - ReflectionTypeLoadException Error running add-migration command in package manager console Add-Migration导致InvalidOperationException错误。 正确的命令是什么? - Add-Migration results in InvalidOperationException error. What is the correct command? add-Migration 错误 没有为此 DbContext 配置数据库提供程序 - add-Migration Error No database provider has been configured for this DbContext 无法使用 add-migration 在 Asp.Net Mvc 项目中添加迁移? - Not able to add migration in the Asp .Net Mvc Project using add-migration? add-migration命令在ApplicationDbContex上失败 - add-migration command fails on ApplicationDbContex 添加迁移命令仅提示“ScriptHalted” - Add-Migration command only prompt “ScriptHalted” .net核心EntityFramework“添加迁移”无效 - .net core EntityFramework “Add-Migration” not working Add-Migration错误:指定的架构无效 - Add-Migration error: Schema specified is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM