Entity Framework Core 2.0 Add-Migration 不创建任何迁移文件
Entity Framework Core 2.0 Add-Migration doesn't create any migration files
最近从 EF Core 1.0 迁移到 EF Core 2.0,运行没问题。今天我添加了一个新的 table(代码优先)并将其添加到我的 DbContext 中:
public virtual DbSet<Foo> Foos { get; set; }
当我 运行 迁移时,我的 DbContext 在一个单独的项目中(记住这一切之前都是有效的),迁移结束,但没有创建迁移文件。这一切都在 Core 2.0 之前使用:
http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html
PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz
Using project 'class.xyz\Mfc.MfcRepositoryModel'.
Using startup project 'web.xyz'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel
Using assembly 'Mfc.MfcRepositoryModel'.
Using startup assembly 'web.xyz'.
Using application base 'C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0'.
Using working directory 'C:\development\xyz\web.xyz'.
Using root namespace 'Mfc.MfcRepositoryModel'.
Using project directory 'C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding BuildWebHost method...
No BuildWebHost method was found on type 'xyz.Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Found DbContext 'MfcDbContext'.
Using DbContext factory 'MfcContextFactory'.
Using context 'MfcDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'web.xyz'...
No design-time services were found.
没有对我来说很明显的错误,也没有创建迁移文件。我已经完成了 Remove-Migration 以开始清理,但仍然没有工作。
您需要将 Program
class 更新为如下所示
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
host.Run();
}
// Tools will use this to get application services
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
从迁移项目中删除所有 IDesignTimeDbContextFactory<TContext>
实现,并在启动 class 的 ConfigureServices
方法中注册 DbContext
,方法与注册时相同运行正在申请。
然后 运行 你的迁移命令像往常一样,这对我来说很管用。
我尝试更新 .csproj 文件中的运行时版本以包括:
<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>
在 PropertyGroup 标记中。它对我有用。
我不得不将一个项目标记为启动项目。
.NET Core class 库中存在问题。成功的解决方法是将以下内容放入 Model
项目的 csproj 文件中:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
只需查看迁移 ModelSnapshot.cs 文件,了解 table 的名称差异、关系创建(在代码末尾)。
我遇到了类似的问题,但 none 提供的答案解决了它。查看我的 .csproj-file
后,我发现 Visual Studio 在某些时候自动添加了以下几行:
<ItemGroup>
<Compile Remove="Migrations\**" />
<Content Remove="Migrations\**" />
<EmbeddedResource Remove="Migrations\**" />
<None Remove="Migrations\**" />
</ItemGroup>
删除此 ItemGroup
后,更新我的数据库再次正常工作。
DbContext class 应该有无参数构造函数和带选项的构造函数。您必须覆盖 OnConfiguring。在它的内部指定了 exaple 的提供者:
options.UseSqlServer(_connectionString);
代码示例:
public class AppDbContext : DbContext
{
private const string _connectionString = "Data Source=....;App=EntityFramework";
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{ }
public AppDbContext() : base()
{
}
public virtual DbSet<MyEntitie> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (options.IsConfigured)
{
return;
}
options.UseSqlServer(_connectionString);
}
}
最后使用命令行和 运行 这个命令(键入项目的文件名,其中是你的 DbContext 文件),或者你可以使用 nuget 包管理器控制台:
dotnet ef migrations -v -p App.csproj add Init
最近从 EF Core 1.0 迁移到 EF Core 2.0,运行没问题。今天我添加了一个新的 table(代码优先)并将其添加到我的 DbContext 中:
public virtual DbSet<Foo> Foos { get; set; }
当我 运行 迁移时,我的 DbContext 在一个单独的项目中(记住这一切之前都是有效的),迁移结束,但没有创建迁移文件。这一切都在 Core 2.0 之前使用:
http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html
PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz
Using project 'class.xyz\Mfc.MfcRepositoryModel'.
Using startup project 'web.xyz'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel
Using assembly 'Mfc.MfcRepositoryModel'.
Using startup assembly 'web.xyz'.
Using application base 'C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0'.
Using working directory 'C:\development\xyz\web.xyz'.
Using root namespace 'Mfc.MfcRepositoryModel'.
Using project directory 'C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding BuildWebHost method...
No BuildWebHost method was found on type 'xyz.Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Found DbContext 'MfcDbContext'.
Using DbContext factory 'MfcContextFactory'.
Using context 'MfcDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'web.xyz'...
No design-time services were found.
没有对我来说很明显的错误,也没有创建迁移文件。我已经完成了 Remove-Migration 以开始清理,但仍然没有工作。
您需要将 Program
class 更新为如下所示
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
host.Run();
}
// Tools will use this to get application services
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
从迁移项目中删除所有 IDesignTimeDbContextFactory<TContext>
实现,并在启动 class 的 ConfigureServices
方法中注册 DbContext
,方法与注册时相同运行正在申请。
然后 运行 你的迁移命令像往常一样,这对我来说很管用。
我尝试更新 .csproj 文件中的运行时版本以包括:
<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>
在 PropertyGroup 标记中。它对我有用。
我不得不将一个项目标记为启动项目。
.NET Core class 库中存在问题。成功的解决方法是将以下内容放入 Model
项目的 csproj 文件中:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
只需查看迁移 ModelSnapshot.cs 文件,了解 table 的名称差异、关系创建(在代码末尾)。
我遇到了类似的问题,但 none 提供的答案解决了它。查看我的 .csproj-file
后,我发现 Visual Studio 在某些时候自动添加了以下几行:
<ItemGroup>
<Compile Remove="Migrations\**" />
<Content Remove="Migrations\**" />
<EmbeddedResource Remove="Migrations\**" />
<None Remove="Migrations\**" />
</ItemGroup>
删除此 ItemGroup
后,更新我的数据库再次正常工作。
DbContext class 应该有无参数构造函数和带选项的构造函数。您必须覆盖 OnConfiguring。在它的内部指定了 exaple 的提供者:
options.UseSqlServer(_connectionString);
代码示例:
public class AppDbContext : DbContext
{
private const string _connectionString = "Data Source=....;App=EntityFramework";
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{ }
public AppDbContext() : base()
{
}
public virtual DbSet<MyEntitie> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (options.IsConfigured)
{
return;
}
options.UseSqlServer(_connectionString);
}
}
最后使用命令行和 运行 这个命令(键入项目的文件名,其中是你的 DbContext 文件),或者你可以使用 nuget 包管理器控制台:
dotnet ef migrations -v -p App.csproj add Init