Search code examples
asp.netpostgresqlasp.net-coreentity-framework-coreaspnetboilerplate

Exception has been thrown by the target of an invocation in asp.net core and postgresql migration


I am trying to create a project. That's why I downloaded a startup template from this link. Then I tried to configure with asp.net core. I set the connection string as "Default": "User ID=postgres;Password=**********;Host=localhost;Port=5432;Database=LibraryManagementDb;Pooling=true;". and then changed UseSqlServer to UseNpgsql in LibraryManagementDbContextConfigurer class.

`

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

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

`

and add the below code to DbContext class

`

 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
    }

`

and removed all migration classes from the Migration folder and run add-migration Initial_Migration command in the package manager console

but I got the Exception has been thrown by the target of an invocation. error

this is the whole exception

`

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.

`

How can I resolve this??? Please help...


Solution

  • By updating 'Npgsql.EntityFrameworkCore.PostgreSQL' solved this problem