ema*_*man 22 c# .net-core asp.net-core .net-core-3.0 .net-core-3.1
在 .net core 中添加数据库迁移时遇到以下错误
这是错误:
这是中的代码Startup
:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllers();
}
Run Code Online (Sandbox Code Playgroud)
这是ApplicationDbContext
课程:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ }
public DbSet<ApplicationUser> applicationUsers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是ApplicationUser
:
public class ApplicationUser : IdentityUser
{
[Required]
[Column(TypeName = "nvarchar(150)")]
public string UserFName { get; set; }
[Required]
public string UserLName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
AFe*_*ter 32
我发现此错误的原因可能是您的代码中的多种原因。至少对我来说,最好的方法是在命令中添加详细信息。
这样就能明白是什么问题了。在verbose
将显示执行的所有步骤。
在视觉工作室中使用:
add-migration Added_something -verbose
Run Code Online (Sandbox Code Playgroud)
对于 CLI 使用:
dotnet ef migrations add Added_something --verbose
Run Code Online (Sandbox Code Playgroud)
小智 15
I had the same error when I had two constructors of my DbContext. After rearranging constructors order to parameterless constructor being first in the class fixed it. e.g.
public ProgramDbContext() : base()
{
}
public ProgramDbContext(DbContextOptions<ProgramDbContext> options) : base(options)
{
}
Run Code Online (Sandbox Code Playgroud)
It must be something with dynamic DbContext object invocation with Reflection playing here.
小智 14
如果选择了多个启动项目,也会发生此错误。我将我的 webproject 设置为启动项目,这为我解决了这个问题。
Ami*_*lle 13
我的问题是通过安装Microsoft.EntityFrameworkCore.Design
nuget 包解决的。
Entity Framework Core Tools需要这个包才能工作。确保您的启动项目正确。然后安装包。
最后在您的项目中Build -> Clean Solution,然后再次尝试运行您的命令。
添加迁移命令cli:
dotnet ef migrations add InitDatabase --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
Run Code Online (Sandbox Code Playgroud)
更新数据库命令cli:
dotnet ef database update InitDatabase --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
Run Code Online (Sandbox Code Playgroud)
删除迁移命令 cli:
dotnet ef migrations remove --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
Run Code Online (Sandbox Code Playgroud)
Entity Framework Core 工具参考 - .NET Core CLI
小智 6
static IHostBuilder CreateHostBuilder(string[] args)
如果您从 .net core 应用程序的 Program.cs 中删除该方法,也可能会发生此错误。(这是我的情况)
小智 5
我今天在运行时也遇到了同样的问题 dotnet ef migrations add <MigrationName>
我有三个项目,MainApp(Web),带有 DBContext 的 C# 项目和用于模型的 C# 项目。
我能够从 CLI 解决它。
dotnet ef migrations add AddCategoryTableToDb -s MainApp -p ProjectHavingDbContext
Run Code Online (Sandbox Code Playgroud)
看来你的继承是错误的。
public ApplicationDbContext : IdentityDbContext
Run Code Online (Sandbox Code Playgroud)
应该
public ApplicationDbContext : IdentityDbContext<ApplicationUser>
Run Code Online (Sandbox Code Playgroud)
或者
public ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole>
Run Code Online (Sandbox Code Playgroud)
如果您还扩展角色类。
IdentityUser
当您想要使用扩展用户类(而不是)创建上下文时
归档时间: |
|
查看次数: |
20412 次 |
最近记录: |