[英]Enabling Migrations in EF core?
我开始使用 EF Core 2.0,我有一个面向 .NET 4.6.1 的控制台应用程序我有一个非常简单的模型类,这个上下文:
public class ContextCore : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["efCoreCon"].ConnectionString);
}
public DbSet<ModelC> Models { get; set; }
}
这是连接字符串:
<add name="efCoreCon" connectionString="server=PC-MSHWF\SQLEXPRESS;database=efCoreDB;integrated security=true;" />
我注意到官方文档中的 ef core 中没有Enable-Migrations
命令
所以我运行Add-migration firstMigration
但我收到了这个错误:
在程序集“NewConsole”中找不到迁移配置类型。 (在 Visual Studio 中,您可以使用包管理器控制台中的 Enable-Migrations 命令添加迁移配置)。
当我尝试 Enable-Migrations 时,出现此错误:
在程序集“NewConsole”中找不到上下文类型。
转到包管理器控制台并使用Install-Package Microsoft.EntityFrameworkCore.Tools
安装所需的工具。 完成后尝试使用命令EntityFrameworkCore\\Add-Migration firstMigration
。
在 powershell CLI 中输入这个 --> dotnet ef migrations add InitialMigration
这将安装正确的核心工具
// Package Manger
PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.0.1
// or this will work inside the CLI Console
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 2.0.1
// **for the current/LATEST ver. leave out version option it will install latest Install-Package Microsoft.EntityFrameworkCore.Tools
修复您的错误问题:
看看这个SO 答案:“你应该只需要更新你的 project.json 文件的工具部分以包含这个:”
"Microsoft.EntityFrameworkCore.Tools": {
"version": "2.0.1", // I corrected this from previous answer for your version
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
}
奖励 :) 要自动运行迁移...在您的主应用程序的 startup.cs 中。
// setup the HTTP request pipeline to check and migrate.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
try
{
using (var migrationSvcScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
migrationSvcScope.ServiceProvider.GetService<EFMigrationsMyDBContext>().Database.Migrate();
// you can also add the data here... let me know if you need I will post it
}
}
... // Rest of the startup stuff
}
使用 C# 7.1 启动 .NET Core 2,你可以有一个异步Main
方法到你的应用程序,所以你可以在运行主机之前调用所有初始化逻辑,在它完成构建之后:
public class Program
{
public static async Task Main(string[] args)
{
//first build
var host = CreateHostBuilder(args).Build();
//initialize
using (var serviceScope = host.Services.CreateScope())
{
var serviceProvider = serviceScope.ServiceProvider;
var isDevelopment =
serviceProvider.GetRequiredService<IWebHostEnvironment>().IsDevelopment();
using var context = serviceProvider.GetRequiredService<AppDbContext>();
if (isDevelopment)
await context.Database.EnsureCreatedAsync();
else
await context.Database.MigrateAsync();
if (isDevelopment)
{
using var userManager =
serviceProvider.GetRequiredService<UserManager<AppUser>>();
await userManager
.CreateAsync(new AppUser { UserName = "dummy", Email = "dummy@dumail.com" },
password: "1234");
}
}
//now run
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
编辑您拥有 EF Core 2.0 的 .csproj 并添加:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
dotnet ef migrations add <<migration's_name>>
。 例如: dotnet ef migrations add Init
。 如果您的启动项目在不同的文件夹中,那么您可以使用--startup-project ../<<other_project_folder>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.