“Dotnet ef 迁移添加”不创建迁移

Posted

技术标签:

【中文标题】“Dotnet ef 迁移添加”不创建迁移【英文标题】:"Dotnet ef migrations add" not creating the migration 【发布时间】:2021-08-22 20:15:49 【问题描述】:

所以我正在尝试更新我的数据库,但实际上并没有创建迁移,如果我命名旧的迁移文件夹,它会更改快照但不会创建实际的迁移, 如果我命名另一个文件夹,则不会创建任何内容。 我仍处于早期开发阶段(完全可以接受删除数据库),所以我试图压缩整个事情。

新尝试(我和我的 DbContext 在同一个项目中)

dotnet ef migrations add Init -o \Migrations\Init -s ..\<StartupLocation>

创建一个文件夹,创建一个快照,但不创建实际的迁移。

dotnet ef migrations list -s ..\<StartupLocation>

一无所获

dotnet ef database update -s ..\<StartupLocation>

创建一个空数据库...

我正在使用 SQLite...

我尝试搜索该问题,但遇到了其他问题,有什么想法可以解决这个问题吗?

清理解决方案并不能解决问题

数据库上下文

public EcomContext( DbContextOptions<EcomContext> options ) : base( options )

//Fix for nullable reference types
public DbSet<SomeClass> SomeClass => Set<SomeClass>();
//Just a string wrapper with int id and string
public DbSet<StringWrapper> StringWrapperers => Set<StringWrapper>();

protected override void OnModelCreating( ModelBuilder modelBuilder )

    modelBuilder.ApplyConfigurationsFromAssembly( 
    Assembly.GetExecutingAssembly() );
    base.OnModelCreating( modelBuilder );

某类

public class SomeClass
    
    public SomeClass( string firstNonNullableValue, string secondNonNullableValue, string thirdNonNullableValue )
    
        FirstNonNullableValue = firstNonNullableValue;
        SecondNonNullableValue = secondNonNullableValue;
        ThirdNonNullableValue = thirdNonNullableValue ;
    

    public Guid Id  get; set; 

    public string FirstNonNullableValue get; set; 

    public string SecondNonNullableValue get; set; 

    public string ThirdNonNullableValue get; set; 

    public string? JustAString  get; set; 
//RangedValue is just a owned type with 2 ints
    public RangedValue? Value  get; set; 
    public RangedValue? Value2  get; set; 
    public SomeEnum? SomeEnum get; set; 
    public IList<StringWrapper>? StringWrappers  get; set; 

    public string? SomeString  get; set; 
    public string? SomeString2  get; set; 


已编辑:包括构建日志

迁移

PS D:\Software\Visual Studio\Projects\Ecom\Ecom.Data> dotnet ef migrations add Tags -s ..\Ecom.API\ -o /migrations/Tags -v
Using project 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\Ecom.Data.csproj'.
Using startup project '..\Ecom.API\Ecom.API.csproj'.
Writing 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\obj\Ecom.Data.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Supremus\AppData\Local\Temp\tmpDD49.tmp /verbosity:quiet /nologo "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\Ecom.Data.csproj"
Writing '..\Ecom.API\obj\Ecom.API.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Supremus\AppData\Local\Temp\tmpDFFA.tmp /verbosity:quiet /nologo ..\Ecom.API\Ecom.API.csproj
Build started...
dotnet build ..\Ecom.API\Ecom.API.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.01
Build succeeded.
dotnet exec --depsfile "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.deps.json" --additionalprobingpath C:\Users\Supremus\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "C:\Program Files (x86)\Microsoft\Xamarin\NuGet" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.runtimeconfig.json" C:\Users\Supremus\.dotnet\tools\.store\dotnet-ef\5.0.7\dotnet-ef\5.0.7\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll migrations add Tags -o /migrations/Tags --assembly "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.Data.dll" --startup-assembly "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.dll" --project-dir "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\\" --language C# --working-dir "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data" --verbose --root-namespace Ecom.Data
Using assembly 'Ecom.Data'.
Using startup assembly 'Ecom.API'.
Using application base 'D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0'.
Using working directory 'D:\Software\Visual Studio\Projects\Ecom\Ecom.API'.
Using root namespace 'Ecom.Data'.
Using project directory 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\'.
Remaining arguments: .
The Entity Framework tools version '5.0.7' is older than that of the runtime '5.0.9'. Update the tools for the latest features and bug fixes.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Ecom.API'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'EcomContext'.
Finding DbContext classes in the project...
Using context 'EcomContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.Sqlite'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.Sqlite'.
Finding design-time services referenced by assembly 'Ecom.API'...
Finding design-time services referenced by assembly 'Ecom.Data'...
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'Ecom.API'...
No design-time services were found.
Reusing namespace of type 'Init'.
An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
Writing migration to 'D:\migrations\Tags\20210823160452_Tags.cs'.
Writing model snapshot to 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\Migrations\Product\EcomContextModelSnapshot.cs'.

更新

PS D:\Software\Visual Studio\Projects\Ecom\Ecom.Data> dotnet ef database update -s ..\Ecom.API\ -v
Using project 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\Ecom.Data.csproj'.
Using startup project '..\Ecom.API\Ecom.API.csproj'.
Writing 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\obj\Ecom.Data.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Supremus\AppData\Local\Temp\tmpFE02.tmp /verbosity:quiet /nologo "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\Ecom.Data.csproj"
Writing '..\Ecom.API\obj\Ecom.API.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Supremus\AppData\Local\Temp\tmpC2.tmp /verbosity:quiet /nologo ..\Ecom.API\Ecom.API.csproj
Build started...
dotnet build ..\Ecom.API\Ecom.API.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.15
Build succeeded.
dotnet exec --depsfile "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.deps.json" --additionalprobingpath C:\Users\Supremus\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "C:\Program Files (x86)\Microsoft\Xamarin\NuGet" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.runtimeconfig.json" C:\Users\Supremus\.dotnet\tools\.store\dotnet-ef\5.0.7\dotnet-ef\5.0.7\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll database update --assembly "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.Data.dll" --startup-assembly "D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0\Ecom.API.dll" --project-dir "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\\" --language C# --working-dir "D:\Software\Visual Studio\Projects\Ecom\Ecom.Data" --verbose --root-namespace Ecom.Data
Using assembly 'Ecom.Data'.
Using startup assembly 'Ecom.API'.
Using application base 'D:\Software\Visual Studio\Projects\Ecom\Ecom.API\bin\Debug\net5.0'.
Using working directory 'D:\Software\Visual Studio\Projects\Ecom\Ecom.API'.
Using root namespace 'Ecom.Data'.
Using project directory 'D:\Software\Visual Studio\Projects\Ecom\Ecom.Data\'.
Remaining arguments: .
The Entity Framework tools version '5.0.7' is older than that of the runtime '5.0.9'. Update the tools for the latest features and bug fixes.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Ecom.API'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'EcomContext'.
Finding DbContext classes in the project...
Using context 'EcomContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.Sqlite'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.Sqlite'.
Finding design-time services referenced by assembly 'Ecom.API'...
Finding design-time services referenced by assembly 'Ecom.Data'...
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'Ecom.API'...
No design-time services were found.
Done.

【问题讨论】:

您确定您的数据库实际上与您的课程不同吗?另请发布您的 dbcontext 和一两个实体类。 您可以运行带有详细标志的 ef 命令并在此处发布日志。例如dotnet ef database update -s ..\&lt;StartupLocation&gt; --verbose @Sam 没问题,在原帖中添加了稍微删减的代码 @FirdausKamaruddin 包括在上面 嗨@BananaSupreme,日志说The Entity Framework tools version '5.0.7' is older than that of the runtime '5.0.9'.。我认为您需要首先使用以下方法更新 ef 工具:dotnet tool update --global dotnet-ef 【参考方案1】:
dotnet ef migrations add Init -o \Migrations\Init -s ..\<StartupLocation>

它缺少一个点,所以它输出到绝对路径而不是相对路径... 应该是

dotnet ef migrations add Init -o .\Migrations\Init -s ..\<StartupLocation>

注意输出文件夹中的点...

【讨论】:

以上是关于“Dotnet ef 迁移添加”不创建迁移的主要内容,如果未能解决你的问题,请参考以下文章

Laravel工匠迁移不创建新表

进行迁移时如何创建浮动但不加倍的字段?

PHP工匠迁移不创建新表

php artisan make:迁移不创建新表

artisan migrate 命令不迁移,不产生输出

通过virt-manager 利用NFS创建迁移虚拟机2