C# 在EF core中启用迁移?
我开始学习EF Core 2.0, 我有一个针对.NET4.6.1的控制台应用程序 我有一个非常简单的模型类,以下是上下文:C# 在EF core中启用迁移?,c#,ef-core-2.0,C#,Ef Core 2.0,我开始学习EF Core 2.0, 我有一个针对.NET4.6.1的控制台应用程序 我有一个非常简单的模型类,以下是上下文: public class ContextCore : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConfigurationManager.Con
public class ContextCore : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["efCoreCon"].ConnectionString);
}
public DbSet<ModelC> Models { get; set; }
}
公共类ContextCore:DbContext
{
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionString[“efCoreCon”].ConnectionString);
}
公共数据库集模型{get;set;}
}
这是连接字符串:
<add name="efCoreCon" connectionString="server=PC-MSHWF\SQLEXPRESS;database=efCoreDB;integrated security=true;" />
所以我运行了addmigrationfirstmigration
但我有一个错误:
在程序集中找不到迁移配置类型
“新控制台”。(在Visual Studio中,可以使用“启用迁移”
来自Package Manager控制台的用于添加迁移的命令
配置)
当我尝试启用迁移时,出现以下错误:
在程序集“NewConsole”中未找到上下文类型
转到软件包管理器控制台,使用安装软件包Microsoft.EntityFrameworkCore.tools
安装所需的工具。完成后,请尝试在powershell CLI中使用命令EntityFrameworkCore\Add Migration firstMigration
键入此-->dotnet ef migrations Add InitialMigration
这使迁移成为可能
这将安装
修复您的错误问题:
看看这个:“您应该只需要更新project.json文件的tools部分,以包含以下内容:”
奖励:)自动运行迁移。。。在主应用程序的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
}
//设置HTTP请求管道以进行检查和迁移。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
尝试
{
使用(var migrationSvcScope=app.ApplicationServices.GetRequiredService()
.CreateScope())
{
migrationSvcScope.ServiceProvider.GetService().Database.Migrate();
//你也可以在这里添加数据…如果你需要,请告诉我,我会发布它
}
}
…//其余的创业材料
}
编辑您拥有EF Core 2.0的.csproj并添加:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
打开Windows PowerShell
转到您拥有EF Core 2.0的目录
键入dotnet ef migrations add使用C#7.1启动.NET Core 2,您的应用程序可以有一个异步Main
方法,因此您可以在运行主机之前调用所有初始化逻辑,就在主机完成构建之后:
公共类程序
{
公共静态异步任务主(字符串[]args)
{
//第一次建造
var host=CreateHostBuilder(args.Build();
//初始化
使用(var serviceScope=host.Services.CreateScope())
{
var serviceProvider=serviceScope.serviceProvider;
var isDevelopment=
serviceProvider.GetRequiredService().IsDevelopment();
使用var context=serviceProvider.GetRequiredService();
if(ISD开发)
等待context.Database.EnsureCreatedAsync();
其他的
wait context.Database.MigrateAsync();
if(ISD开发)
{
使用var userManager=
serviceProvider.GetRequiredService();
等待用户管理器
.CreateAsync(新AppUser{UserName=“dummy”,电子邮件=”dummy@dumail.com" },
密码:“1234”);
}
}
//快跑
host.Run();
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
});
}
您正在使用EF非核心命令。使用dotnet ef…
。您的上下文的名称空间是什么?这里有一个简单的教程@pablotondoldavargas NewConsole.EFCoreI使用dotnet ef migrations add InitialCreate
,但是得到:dotnet:找不到匹配命令“dotnet ef”的可执行文件…对我来说,只需安装软件包并重新启动Visual Studio就足以解决此问题
// 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
}
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>