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

Typeprovider not working when compiled with dotnet build #807

Closed
afaayerhan opened this issue Nov 23, 2023 · 11 comments
Closed

Typeprovider not working when compiled with dotnet build #807

afaayerhan opened this issue Nov 23, 2023 · 11 comments

Comments

@afaayerhan
Copy link

Describe the bug
In Vscode the typeprovider works with all necessary Entity's autocompleted from the database. but when you run dotnet build it fails to compile with following error.

/Users/afaayerhan/projects/rufworks/FsWebPlayground/DataCtx.fs(10,20): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. [/Users/afaayerhan/projects/rufworks/FsWebPlayground/FsWebPlayground.fsproj] /Users/afaayerhan/projects/rufworks/FsWebPlayground/DataCtx.fs(10,20): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. [/Users/afaayerhan/projects/rufworks/FsWebPlayground/FsWebPlayground.fsproj] /Users/afaayerhan/projects/rufworks/FsWebPlayground/DataCtx.fs(15,22): error FS0039: The type 'Object' does not define the field, constructor or member 'GetDataContext'. [/Users/afaayerhan/projects/rufworks/FsWebPlayground/FsWebPlayground.fsproj]

here is the code in DataCtx.fs file :
namespace FsWebPlayground.DataCtx
fsharp open System open System.Linq open FSharp.Data.Sql module MyModule = let [<Literal>] dbVendor = Common.DatabaseProviderTypes.POSTGRESQL let [<Literal>] connString = "Username=postgres;Host=localhost;Database=payrolldb;Password=;Port=5432" let [<Literal>] owner = "public, identity" type SqlProv = SqlDataProvider< DatabaseVendor = Common.DatabaseProviderTypes.POSTGRESQL, ConnectionString = connString, Owner = owner > let dc = SqlProv.GetDataContext()
here is my fsproj:
`

net6.0 true ` **To Reproduce** Steps to reproduce the behavior: use postgresql with similar code I posted before, replace with your own connection string

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
Screenshot 2023-11-23 at 18 04 59

Screenshot 2023-11-23 at 18 09 14

Deskstop (please complete the following information):

  • OS: [e.g. macOS]
  • IDE: [e.g. Visual Studio Code working autocomplete] but compilation on cli not working. also in Rider too not working
  • .NET SDK Version: [e.g. 8.0]
@crimsonhawk47
Copy link

Also experiencing this on windows

@Thorium
Copy link
Member

Thorium commented Dec 29, 2023

This is probably because Postgres driver is loaded via reflection and they have added dependency to a component, in this case Microsoft.Extensions.Logging.Abstractions.
Two possible fixes to try;

  1. Set ResolutionPath static parameter and add the missing dll to the path it's pointing to. https://fsprojects.github.io/SQLProvider/core/parameters.html#ResolutionPath
  2. Or, add that the corresponding NuGet package reference (Microsoft.Extensions.Logging.Abstractions) to your project.

@crimsonhawk47
Copy link

I did both of these and it still doesn't work. Now, for the resolution path, do I want it pointing to the folder with Npgsql.dll? One that I installed with nuget?

@Thorium
Copy link
Member

Thorium commented Dec 31, 2023

You have to point ResolutionPath to a folder where you have copied the reference dlls, so not only Npgsql.dll but also the missing (Microsoft.Extensions.Logging.Abstractions.dll).

@crimsonhawk47
Copy link

Yes, I have done this but it still doesn't work

@Thorium
Copy link
Member

Thorium commented Jan 2, 2024

Which version of Npgsql started the issue? I guess 6.0.10 didn't had the issue?

@crimsonhawk47
Copy link

Which version of Npgsql started the issue? I guess 6.0.10 didn't had the issue?

It doesn't matter what version of Npgsql I use. I have removed the resolution path for now because it wasn't making a difference. I can send a zip of the project if that would be helpful

image
image

@Thorium
Copy link
Member

Thorium commented Jan 11, 2024

This is a different error. Another one was about dotnet.exe and Microsoft.Extensions.Logging.Abstraction.dll missing.

This is about System.Runtime where it seems your VS is running on .NET Framework 4.7.2 and your references are .NET 6.0.

@crimsonhawk47
Copy link

This is a different error. Another one was about dotnet.exe and Microsoft.Extensions.Logging.Abstraction.dll missing.

This is about System.Runtime where it seems your VS is running on .NET Framework 4.7.2 and your references are .NET 6.0.

Yes I'm getting different linting errors depending on using Visual Studio vs Jetbrains Rider.

My program.fs file is targeting 8.0, and my references don't have anything about .NET framework.

Would you be willing to send me a zip of a starter where this is working and just let me change the connection string? I have put the DLLs in a folder and set the resolution path but I can't for the life of me get it working.

@Thorium
Copy link
Member

Thorium commented Jan 11, 2024

Ok, it seems there are multiple issues and all of them addressed now:

  1. The new Npgsql (or Postgers 16) had changed the mapping from array_dimensions from int32 to int16 (short). That caused "Invalid cast exception". I had to release new version of SQLProvider 1.3.24 to fix that.

  2. Npgsql had added also dependency to System.Text.Json which was not clear from the error-message.

  3. Your editor (e.g. Visual Studio 2022) might run on .NET Framework 4.7.2 even if you are developing to .NET 8.0. (It's just because VS is an old program.) For that reason, you need to select your ResolutionPath files as netstandard2.0 versions of files. Get them e.g. from your Nuget cache folder. Net Framework 4.7.2 is compatible with .NET Standard 2.0. But don't worry, they are just design-time usage for VS. The runtime execution time is whatever you are developing to (e.g. .NET 8.0). This is because type-provider does operate both on runtime and design-time/compile-time.

By addressing these issues, you should be able to run SQLProvider on your .NET 8.0 project with all the latest referencies.

If you are interested of a working zip-file, having a readme.md how I made it, it's here:
myTest.zip

@crimsonhawk47
Copy link

crimsonhawk47 commented Jan 12, 2024

Thank you for this. I didn't realize the linter might not react when I change the resolutionPath. It requires an IDE restart, which made it unclear what I was doing was working.

I ended up not needing anything except Microsoft.Extensions.Logging.Abstractions in my resolutionPath (not even Npgsql.dll) and the 8.0 version was fine for Rider. Maybe I'll try the framework for Visual Studio but I don't need it. You can close this now.

@Thorium Thorium closed this as completed Jan 12, 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

3 participants