【问题标题】:Exception has been thrown by the target of an invocation in asp.net core and postgresql migrationasp.net core 和 postgresql 迁移中的调用目标引发了异常
【发布时间】:2021-05-16 06:24:19
【问题描述】:

我正在尝试创建一个项目。这就是我从this link 下载启动模板的原因。然后我尝试使用 asp.net core 进行配置。我将连接字符串设置为"Default": "User ID=postgres;Password=**********;Host=localhost;Port=5432;Database=LibraryManagementDb;Pooling=true;"。然后在 LibraryManagementDbContextConfigurer 类中将UseSqlServer 更改为UseNpgsql

`

public static void 
     Configure(DbContextOptionsBuilder<LibraryManagementDbContext> 
      builder, string connectionString)
        {
          builder.UseNpgsql(connectionString);
     }

    public static void 
     Configure(DbContextOptionsBuilder<LibraryManagementDbContext> 
       builder, DbConnection connection)
    {
        builder.UseNpgsql(connection);
    }

`

并将以下代码添加到 DbContext 类中

`

 protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationLanguageText>()
            .Property(p => p.Value)
            .HasMaxLength(100); // any integer that is smaller than 10485760
    }

`

并从 Migration 文件夹中删除所有迁移类,并在包管理器控制台中运行 add-migration Initial_Migration 命令

但我得到 异常已被调用目标抛出。错误

这是整个例外

`

PM> add-migration Initial_Migration
Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been 
thrown by the target of an invocation.
---> System.TypeLoadException: Method 'get_Info' in type  
'Microsoft.EntityFrameworkCore.Infrastructure.
Internal.NpgsqlOptionsExt 
  ension' from assembly 'Npgsql.EntityFrameworkCore.PostgreSQL, 
Version=2.1.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' 
does not have an implementation.
at Microsoft.EntityFrameworkCore
.NpgsqlDbContextOptionsExtensions.UseNpgsql[TContext]
   at LibraryManagement.EntityFrameworkCore
.LibraryManagementDbContextConfigurer
.Configure(DbContextOptionsBuilder`1 builder, String 
connectionString) in D:\6.0.0\aspnet- 
core\src\LibraryManagement.EntityFrameworkCore
\EntityFrameworkCore\LibraryManagementDbContextConfigurer.cs:line 
 10
 at LibraryManagement.EntityFrameworkCore
.LibraryManagementDbContextFactory.CreateDbContext(String[] args) 
in D:\6.0.0\aspnet-core\src\LibraryManagement.EntityFrameworkCore
\EntityFrameworkCore\LibraryManagementDbContextFactory.cs:line 17
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 
arguments, Signature sig, Boolean constructor, Boolean 
wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, 
BindingFlags invokeAttr, Binder binder, Object[] parameters, 
CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] 
parameters)
at Microsoft.EntityFrameworkCore.Design
.Internal.DbContextOperations.CreateContextFromFactory(Type 
factory, 
Type contextType)
at 
Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations. 
<>c__DisplayClass16_0.<FindContextFactory>b__1()
at Microsoft.EntityFrameworkCore.Design.Internal
.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal
.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal
.MigrationsOperations.AddMigration(String name, String outputDir, 
String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor
.AddMigrationImpl(String name, String outputDir, String 
contextType, 
String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor
.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor
.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor
.OperationBase.Execute(Action action)
Exception has been thrown by the target of an invocation.

`

我该如何解决这个问题???请帮忙...

【问题讨论】:

  • 你的 'EntityFramework' 和 'Npgsql.EntityFrameworkCore.PostgreSQL' 的版本是什么?
  • 'Npgsql.EntityFrameworkCore.PostgreSQL' 版本是 5.0.2(最新),但不知道 'EntityFramework' 的版本。但我在我的电脑上安装了.net core,版本是 5.0.103。不确定这是“EntityFramework”版本吗?或者如何检查“EntityFramework”版本?
  • Entity Framework 6 和 Entity Framework Core 都安装好了

标签: asp.net postgresql asp.net-core entity-framework-core aspnetboilerplate


【解决方案1】:

通过更新 'Npgsql.EntityFrameworkCore.PostgreSQL' 解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2021-12-27
    • 2021-03-08
    相关资源
    最近更新 更多