npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.54k stars 226 forks source link

Exception when upgrading Npgsql to Version 5.0.0-preview1.1 #1535

Closed weitzhandler closed 4 years ago

weitzhandler commented 4 years ago

Hi,

It seems EF Core tools doesn't play well with the new version. In my project, as soon as I upgrade Npgsql to version 5.0.0-preview1.1 (from 5.0.0-alpha1), I get the following exception:

  Message: 
        [Test Collection Cleanup Failure (ApiIntegrationTestsCollection)]: System.MissingMethodException : Method not found: 'System.String System.Data.Common.DbException.get_SqlState()'.
      Stack Trace: 
        <<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
        AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
        <>c__DisplayClass170_0.<DoReadMessage>g__ReadMessageLong|0(DataRowLoadingMode dataRowLoadingMode2, Boolean readingNotifications2, Boolean isReadingPrependedMessage, CancellationToken cancellationToken2)
        NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, CancellationToken cancellationToken)
        NpgsqlConnector.ReadMessage(Boolean async, CancellationToken cancellationToken)
        NpgsqlConnector.Authenticate(String username, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
        NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
        <<Open>g__OpenAsync|0>d.MoveNext()
        --- End of stack trace from previous location ---
        NpgsqlConnection.Open()
        NpgsqlDatabaseCreator.Exists()
        RelationalDatabaseCreator.EnsureDeleted()
        DatabaseFacade.EnsureDeleted()
        ApiIntegrationTestsFixture.Dispose(Boolean disposing) line 63
        WebApplicationFactory`1.Dispose()

Here's are the packages installed in my project, either one of the following changes triggers the error:

<ItemGroup>
  <PackageReference Include="FluentValidation" Version="9.2.2" />
  <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0-rc.2.20475.17" />
  <PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
  <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0-rc.2.20475.17" />    
  <PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.0-preview" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0-rc.2.20475.6">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
-  <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="5.0.0-rc1" />
+  <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="5.0.0-rc2" />
-  <PackageReference Include="Npgsql.Json.NET" Version="5.0.0-alpha1" />
+  <PackageReference Include="Npgsql.Json.NET" Version="5.0.0-preview1.1" />
  <PackageReference Include="Weitzhandler.FluentValidaiton.EntityFrameworkCore" Version="0.2.0" />
</ItemGroup>
roji commented 4 years ago

Thanks for filing, but I couldn't repro this... Please see my full csproj and Program.cs file below. The csproj is partial, so there may be something missing (e.g. I don't know what TFM you're targeting). Can you please help arrive at a repro minimal console program?

csproj:

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

  <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="5.0.0-rc2" />
      <PackageReference Include="Npgsql.Json.NET" Version="5.0.0-preview1.1" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0-rc.2.20475.6">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0-rc.2.20475.5" />
  </ItemGroup>

</Project>
Program.cs ```c# public class Program { static async Task Main(string[] args) { await using var ctx = new BlogContext(); await ctx.Database.EnsureDeletedAsync(); await ctx.Database.EnsureCreatedAsync(); } } public class BlogContext : DbContext { public DbSet Blogs { get; set; } static ILoggerFactory ContextLoggerFactory => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information)); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseNpgsql(@"Host=localhost;Username=test;Password=test") .EnableSensitiveDataLogging() .UseLoggerFactory(ContextLoggerFactory); } public class Blog { public int Id { get; set; } public string Name { get; set; } } ```
weitzhandler commented 4 years ago

Hey @roji and thanks for taking the time investigating this.

Could this be the problem? I'm on VS 16.7* and using global.json:

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.7.20366.6
 Commit:    0684df3a5b

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.7.20366.6\

Host (useful for support):
  Version: 5.0.0-rc.2.20475.5
  Commit:  c5a3f49c88

Anyway, since this isn't a blocker to me and I don't mind using the lower version, and especially since you're unable to repro the problem, I'm gonna close this issue.

roji commented 4 years ago

Visual Studio 16.8, Preview 4 is required to use .NET Core 5.0 rc2 (see https://dotnet.microsoft.com/download/dotnet/5.0), so that could indeed be the source of the trouble.

However, can I ask you to give this a few more minutes and see if the issue reproduces via dotnet CLI (outside VS), or in the latest VS preview? If there's an actual Npgsql issue here it would be good to know about it.

weitzhandler commented 4 years ago

The thing is that I have side by side and 16.8 preview is installed too. I'll try to change the global.json and try to run in VS 16.8p4.

weitzhandler commented 4 years ago

After setting the global.json to the rc2 sdk, it worked fine. Sorry for the hassle and thanks for your diligence @roji.

roji commented 4 years ago

Thanks for looking into it and confirming @weitzhandler.