EF Core - 表 '*.__EFMigrationsHistory' 不存在 [英] EF Core - Table '*.__EFMigrationsHistory' doesn't exist
问题描述
我想强调的是,这是 .NET Core,有关 EF 6.0 的线程不适用于此问题
我创建了我的 DbContext 并将其添加到 DI 中,但是当我执行 dotnet ef database update -v
时,它不想创建迁移表 __EFMigrationsHistory
.>
是否有其他命令我应该先执行,或者这是 EF Core MySQL 适配器的错误?
MainDbContext
使用 Microsoft.EntityFrameworkCore;使用 MySQL.Data.EntityFrameworkCore.Extensions;使用 Web.Models;命名空间 Web.Infrastructure{公共类 MainDbContext : DbContext{公共数据库集<用户>用户{得到;放;}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseMySQL("connection-string-here");base.OnConfiguring(optionsBuilder);}}}
错误
<块引用>正在查找 DbContext 类...
使用上下文MainDbContext".
在服务器localhost"上使用数据库db".
MySql.Data.MySqlClient.MySqlException: 表 'db.__EFMigrationsHistory' 不存在
表 'db.__EFMigrationsHistory' 不存在```
project.json
<代码>{依赖关系":{Microsoft.NETCore.App":{版本":1.0.1",类型":平台"},"Microsoft.AspNetCore.Diagnostics": "1.0.0","Microsoft.AspNetCore.Mvc": "1.0.1",Microsoft.AspNetCore.Razor.Tools":{"version": "1.0.0-preview2-final",类型":构建"},"Microsoft.AspNetCore.Routing": "1.0.1","Microsoft.AspNetCore.Server.IISIntegration": "1.0.0","Microsoft.AspNetCore.Server.Kestrel": "1.0.1","Microsoft.AspNetCore.StaticFiles": "1.0.0","Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0","Microsoft.Extensions.Configuration.Json": "1.0.0","Microsoft.Extensions.Logging": "1.0.0","Microsoft.Extensions.Logging.Console": "1.0.0","Microsoft.Extensions.Logging.Debug": "1.0.0","Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0","Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0","BundlerMinifier.Core": "2.2.301","WebMarkupMin.AspNetCore1": "2.2.1","MySql.Data.EntityFrameworkCore": "7.0.6-IR31","Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"},工具": {"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final","Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final","Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"},构架": {netcoreapp1.0":{进口":["dotnet5.6",便携-net45+win8"]}},构建选项":{emitEntryPoint":真,保留编译上下文":真},运行时选项":{配置属性":{System.GC.Server":真}},发布选项":{包括": ["wwwroot","**/*.cshtml","appsettings.json",网络配置"]},脚本":{"prepublish": [ "bower install", "dotnet bundle" ],"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"]}}
临时解决方案
通过执行 dotnet ef migrations script
我得到了可以直接在 MySQL 中执行的 SQL 代码.在创建迁移表之后,一切正常.这是不好的临时解决方案.我仍然想知道启用迁移的正确"方式是什么.
将 Mark G 的评论转化为答案.
创建 __EFMigrationsHistory 表后,应该运行其余的更新.
CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`));
或者,生成迁移脚本并在包管理器控制台中使用此命令手动应用于数据库:
脚本迁移
如果需要生成All脚本,可以使用这个命令:
Script-Migration -from 0
I want to stress out that this is .NET Core and the threads about EF 6.0 does not apply to this problem
I created my DbContext and added it in DI, however when I do dotnet ef database update -v
it does not want to create the migrations table __EFMigrationsHistory
.
Is there some other command that I should do first or this is a bug of EF Core MySQL adapter?
MainDbContext
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using Web.Models;
namespace Web.Infrastructure
{
public class MainDbContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("connection-string-here");
base.OnConfiguring(optionsBuilder);
}
}
}
Error
Finding DbContext classes...
Using context 'MainDbContext'.
Using database 'db' on server 'localhost'.
MySql.Data.MySqlClient.MySqlException: Table 'db.__EFMigrationsHistory' doesn't exist
Table 'db.__EFMigrationsHistory' doesn't exist ```
project.json
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"BundlerMinifier.Core": "2.2.301",
"WebMarkupMin.AspNetCore1": "2.2.1",
"MySql.Data.EntityFrameworkCore": "7.0.6-IR31",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Temp Solution
By executing dotnet ef migrations script
I get SQL code that I can execute directly in MySQL. After that migrations table is created and everything works normally. This is temp-solution which is bad. I still wonder what is the "correct" way of enabling migrations.
Turning Mark G's comment into an answer.
Once the __EFMigrationsHistory table has been created, the rest of the update should run.
CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) );
Alternatively, generate the script of your migration(s) and apply to the database manually using this command in Package Manager Console:
Script-Migration
If you need to generate All scripts, you can use this command:
Script-Migration -from 0
这篇关于EF Core - 表 '*.__EFMigrationsHistory' 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!