efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Trying to get Npgsql working with mvc music store sample..

Open sirentek opened this issue 10 years ago • 7 comments

Hi,

I just try to run Npgsql with Ef Core using MVC. Music store sample is used in my tests.

I execute "dotnet ef update database" and it produces the following exception:

System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator..ctor(Microsoft.EntityFrameworkCore.Metadata.IModel, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Migrations.IMigrationsModelDiffer, Microsoft.EntityFrameworkCore.Migrations.IMigrationsSqlGenerator)'.
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String startupProject, String environment, Action`1 reporter)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator..ctor(Microsoft.EntityFrameworkCore.Metadata.IModel, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Migrations.IMigrationsModelDiffer, Microsoft.EntityFrameworkCore.Migrations.IMigrationsSqlGenerator)'.

I am making the tests with linux. The sample works with Sqlite in Windows as expected. I use dotnet version: 1.0.0-rc2-002611 (which I downloaded yesterday.)

Part of my project.json:

    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0-*",
    "Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.0.0-*"

Part of my NuGet.config:

    <add key="Npgsql" value="https://www.myget.org/F/npgsql-unstable" />

Part of my Startup.cs:

                services.AddDbContext<MusicStoreContext>(options =>
                    options.UseNpgsql(Configuration[StoreConfig.ConnectionStringKey.Replace("__", ":")]));

Any ideas ? Thanks!

sirentek avatar May 04 '16 14:05 sirentek

Now we know the reason about this exception.. There is a missing parameter (migration executer) in constructor : Npgsql.EntityFrameworkCore.PostgreSQL/src/Npgsql.EntityFrameworkCore.PostgreSQL/Storage/Internal/NpgsqlDatabaseCreator.cs

I don't know how to solve it.. I will continue to read/understand..

sirentek avatar May 06 '16 13:05 sirentek

Hi! @roji, I added the required parameter to the constructor. Do you think it may help ? I will compile it and test.

        public NpgsqlDatabaseCreator(
            [NotNull] NpgsqlRelationalConnection connection,
            [NotNull] IMigrationsModelDiffer modelDiffer,
            [NotNull] IMigrationsSqlGenerator migrationsSqlGenerator,
            [NotNull] IMigrationCommandExecutor migrationCommandExecutor,
            [NotNull] IModel model,
            [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder)
            : base(model, connection, modelDiffer, migrationsSqlGenerator, migrationCommandExecutor)
        {
            Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder));

            _connection = connection;
            _migrationsSqlGenerator = migrationsSqlGenerator;
            _rawSqlCommandBuilder = rawSqlCommandBuilder;
        }

sirentek avatar May 06 '16 13:05 sirentek

@sirentek thanks for diving into this!

I looked, and it seems that the additional parameter (the IMigrationCommandExecutor) isn't present in EFCore's release branch (which is rc2), only in dev (which is rc2), the precise commit where they added it is ce07379ec2d5e5e6d3e6077267bcd7c1dde58f92. So a mismatch has probably occurred - you're trying to use Npgsql's EFCore provider (which is rc2) with rc3 EFCore...

I've removed Microsoft's myget aspnetcidev feed to make sure that when compiling Npgsql.EFCore there's no rc3 anywhere, but the project.json already explicitly depends on -rc2- only, so this shouldn't be happening. Take a look at exactly which dependencies are being taken when you build your application, and please let me know!

roji avatar May 06 '16 16:05 roji

You are right. The sample uses float dependencies (1.0.0-*) in project.json. As a result all packages including entity framework used rc3 versions.

In my second test, I modified the NuGet.Config file of the sample as:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="AspNetRelease" value="https://www.myget.org/F/aspnetrelease/api/v3/index.json" />

    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
    <add key="Npgsql" value="https://www.myget.org/F/npgsql-unstable/" />
  </packageSources>
</configuration>

As you can see, I replaced the aspnetcidev with aspnetrelease. This time I didn't see any of rc3 versions during the restore.

After running dotnet ef run, it produced the following exception:

System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator..ctor(Microsoft.EntityFrameworkCore.Metad
ata.IModel, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Migrations.IMigrationsModelDiffer, Microsoft.EntityFramew
orkCore.Migrations.IMigrationsSqlGenerator)'.
   at Microsoft.EntityFrameworkCore.Storage.Internal.NpgsqlDatabaseCreator..ctor(NpgsqlRelationalConnection connection, IMigrationsModelDiffer modelDiffer, IMig
rationsSqlGenerator migrationsSqlGenerator, IModel model, IRawSqlCommandBuilder rawSqlCommandBuilder)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String st
artupProject, String environment, Action`1 reporter)
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

It seems, it is not an easy task for me to fix the constructor parameters. It ends up with dependency problems (rc2->rc3).

sirentek avatar May 06 '16 21:05 sirentek

Am not 100% sure about this, but I think you may still be using rc3 packages cached inside .nuget in your home directory... Can you try specifying rc2 explicitly in your project.json? Another option is deleting your .nuget dir and restoring again after having changed your NuGet.Config.

roji avatar May 06 '16 22:05 roji

Replaced all version names with explicit rc2 ones (1.0.0-rc2-*) in project.json. Deleted all packages under home .nuget directory (more than 12GB). Restored again (still using aspnetrelease feed in Nuget.Config.) Error : Unable to resolve 'Microsoft.EntityFrameworkCore.Tools (>= 1.0.0-rc2-3002543)' for '.NETCoreApp,Version=v1.0'.

I uninstalled my dotnet/cli (it was 1.0.0.2543) and installed the latest one (1.0.0.2697). Again deleted all the packages under home .nuget directory. Restored with success.

Migration files were created without any problem. I login as admin and edit the records from the sample. It works like a charm!

Thanks @roji ! I am closing this issue.

sirentek avatar May 07 '16 07:05 sirentek

Ah, the pleasures of fighting with Microsoft prerelease software :)

Glad it worked out.

roji avatar May 07 '16 10:05 roji