fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
564 stars 144 forks source link

Typeprovider not working when compiled with dotnet build #807

Closed afaayerhan closed 5 months ago

afaayerhan commented 7 months ago

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 [] dbVendor = Common.DatabaseProviderTypes.POSTGRESQL let [] connString = "Username=postgres;Host=localhost;Database=payrolldb;Password=;Port=5432" let [] 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):

crimsonhawk47 commented 6 months ago

Also experiencing this on windows

Thorium commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

Thorium commented 6 months ago

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

crimsonhawk47 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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.