在 EF 核心中启用迁移?
Enabling Migrations in EF core?
我开始使用 EF Core 2.0,
我有一个针对 .NET 4.6.1 的控制台应用程序
我有一个非常简单的模型 class,这个上下文:
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;" />
我注意到 official docs
的 ef 核心中没有 Enable-Migrations
的命令
所以我运行Add-migration firstMigration
但我收到了这个错误:
No migrations configuration type was found in the assembly
'NewConsole'. (In Visual Studio you can use the Enable-Migrations
command from Package Manager Console to add a migrations
configuration).
当我尝试 Enable-Migrations 时,出现了这个错误:
No context type was found in the assembly 'NewConsole'.
转到程序包管理器控制台并使用 Install-Package Microsoft.EntityFrameworkCore.Tools
安装所需的工具。完成后尝试使用命令 EntityFrameworkCore\Add-Migration firstMigration
.
在 powershell CLI 中输入 --> dotnet ef migrations add InitialMigration
这将启用迁移。
这将安装 correct core tools
// 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
修复您的错误问题:
看看这个 :“你应该只需要更新你的 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
}
在您拥有 EF Core 2.0 的位置编辑您的 .csproj 并添加:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
- 打开WindowsPowerShell
- 转到您拥有 EF Core 2.0 的目录
- 键入
dotnet ef migrations add <<migration's_name>>
。例如:dotnet ef migrations add Init
。如果您的启动项目在不同的文件夹中,那么您可以使用 --startup-project ../<<other_project_folder>>
使用 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, 我有一个针对 .NET 4.6.1 的控制台应用程序 我有一个非常简单的模型 class,这个上下文:
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;" />
我注意到 official docs
的 ef 核心中没有Enable-Migrations
的命令
所以我运行Add-migration firstMigration
但我收到了这个错误:
No migrations configuration type was found in the assembly 'NewConsole'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
当我尝试 Enable-Migrations 时,出现了这个错误:
No context type was found in the assembly 'NewConsole'.
转到程序包管理器控制台并使用 Install-Package Microsoft.EntityFrameworkCore.Tools
安装所需的工具。完成后尝试使用命令 EntityFrameworkCore\Add-Migration firstMigration
.
在 powershell CLI 中输入 --> dotnet ef migrations add InitialMigration
这将启用迁移。
这将安装 correct core tools
// 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
修复您的错误问题:
看看这个
"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
}
在您拥有 EF Core 2.0 的位置编辑您的 .csproj 并添加:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
- 打开WindowsPowerShell
- 转到您拥有 EF Core 2.0 的目录
- 键入
dotnet ef migrations add <<migration's_name>>
。例如:dotnet ef migrations add Init
。如果您的启动项目在不同的文件夹中,那么您可以使用--startup-project ../<<other_project_folder>>
使用 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>();
});
}