Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core+ Code First迁移时报错,会是什么问题呢? #284

Open
lampo1024 opened this issue Dec 27, 2024 · 13 comments
Open

EF Core+ Code First迁移时报错,会是什么问题呢? #284

lampo1024 opened this issue Dec 27, 2024 · 13 comments

Comments

@lampo1024
Copy link

使用dotnet ef migrations add InitialCreate迁移时报错,如下:

Build started...
Build succeeded.
System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator..ctor(Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly, Microsoft.EntityFrameworkCore.Migrations.IHistoryRepository, Microsoft.EntityFrameworkCore.Storage.IDatabaseCreator, Microsoft.EntityFrameworkCore.Migrations.IMigrationsSqlGenerator, Microsoft.EntityFrameworkCore.Storage.IRawSqlCommandBuilder, Microsoft.EntityFrameworkCore.Migrations.IMigrationCommandExecutor, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Storage.ISqlGenerationHelper, Microsoft.EntityFrameworkCore.Infrastructure.ICurrentDbContext, Microsoft.EntityFrameworkCore.Infrastructure.IModelRuntimeInitializer, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger1<Migrations>, Microsoft.EntityFrameworkCore.Diagnostics.IRelationalCommandDiagnosticsLogger, Microsoft.EntityFrameworkCore.Storage.IDatabaseProvider)'. at ShardingCore.EFCores.ShardingMigrator..ctor(IShardingRuntimeContext shardingRuntimeContext, IMigrationsAssembly migrationsAssembly, IHistoryRepository historyRepository, IDatabaseCreator databaseCreator, IMigrationsSqlGenerator migrationsSqlGenerator, IRawSqlCommandBuilder rawSqlCommandBuilder, IMigrationCommandExecutor migrationCommandExecutor, IRelationalConnection connection, ISqlGenerationHelper sqlGenerationHelper, ICurrentDbContext currentContext, IModelRuntimeInitializer modelRuntimeInitializer, IDiagnosticsLogger1 logger, IRelationalCommandDiagnosticsLogger commandLogger, IDatabaseProvider databaseProvider)
......

会是什么问题呢?

@xuejmnet
Copy link
Collaborator

@lampo1024 efcore几 shardingcore几 配置可以发一下吗

@lampo1024
Copy link
Author

以下是项目配置:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
    <PackageReference Include="ShardingCore" Version="7.8.1.22" />
  </ItemGroup>

</Project>

@xuejmnet
Copy link
Collaborator

@lampo1024 再等会可以吗 efcore9目前还没适配,可以先用8我最近有空了会支持9,年底了比较忙一直没有太多时间

@lampo1024
Copy link
Author

行,我把版本降低一点试试

@lampo1024
Copy link
Author

降低版本之后也有问题:

pwshdotnet ef migrations add InitialCreate
Build started...
Build succeeded.
Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Method 'get_LockReleaseBehavior' in type 'Microsoft.EntityFrameworkCore.SqlServer.Migrations.Internal.SqlServerHistoryRepository' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=8.0.11.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
pwshdotnet ef migrations add InitialCreate
Build started...
Build succeeded.
Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Method 'get_LockReleaseBehavior' in type 'Microsoft.EntityFrameworkCore.SqlServer.Migrations.Internal.SqlServerHistoryRepository' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=7.0.20.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

@xuejmnet
Copy link
Collaborator

@lampo1024 efcore8吗

@lampo1024
Copy link
Author

@lampo1024 efcore8吗

是的

@xuejmnet
Copy link
Collaborator

shardingcore的startup配置方便看看吗

@lampo1024
Copy link
Author

builder.Services.AddShardingDbContext<ReportContext>().UseRouteConfig(builder =>
{
    builder.AddShardingDataSourceRoute<SpecimenReportsVirtualTableRoute>();
}).UseConfig(op =>
{
    ILoggerFactory efLogger = LoggerFactory.Create(builder =>
    {
        builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
    });

    op.UseShardingQuery((conStr, builder) =>
    {
        builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
    });
    op.UseShardingTransaction((connection, builder) =>
    {
        builder.UseSqlServer(connection).UseLoggerFactory(efLogger);
    });
    op.AddDefaultDataSource("2020", "Server=localhost;Database=ReportDB2020;UID=sa;PWD=123;TrustServerCertificate=True;");
    op.AddExtraDataSource(sp =>
    {
        var dbs = new Dictionary<string, string> {
            { "2023","Server=localhost;Database=ReportDB2023;UID=sa;PWD=123;TrustServerCertificate=True;"},
            { "202x","Server=localhost;Database=ReportDB202x;UID=sa;PWD=123;TrustServerCertificate=True;"}
        };
        return dbs;
    });
}).AddShardingCore();

@xuejmnet
Copy link
Collaborator

@lampo1024 目前来看缺少一个useShardingMigrationConfigure,但是我不确定是不是这个问题导致的

@lampo1024
Copy link
Author

我加上试试

@xuejmnet
Copy link
Collaborator

嗯还是不行的话,如果可以方便给一个最小demo吗

@lampo1024
Copy link
Author

晚些时候或者明天我给个demo给你

xuejmnet added a commit that referenced this issue Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants