Skip to content

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Data.SqlClient.SqlConnection' #580

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

Open
mvperez opened this issue Oct 7, 2018 · 8 comments
Labels
.NET Core reference assembly loading Not a bug, but a question about usability with .NET Core. Right dlls are not in ResolutionPath.

Comments

@mvperez
Copy link

mvperez commented Oct 7, 2018

Description

I'm getting this error when I execute "dotnet run"

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Data.SqlClient.SqlConnection' from assembly 'System.Data.SqlClient, Version=4.2.0.0, Culture=neutra
l, PublicKeyToken=b03f5f7f11d50a3a'.
   at FSharp.Data.Sql.Providers.MSSqlServerProvider.FSharp-Data-Sql-Common-ISqlProvider-CreateConnection(String connectionString)
   at <StartupCode$FSharp-Data-SqlProvider>.$SqlRuntime.DataContext.-ctor@37-54.Invoke(String typeName)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at FSharp.Data.Sql.Runtime.SqlDataContext..ctor(String typeName, String connectionString, DatabaseProviderTypes providerType, String resolutionPath, String[] referencedAss
emblies, String runtimeAssembly, String owner, CaseSensitivityChange caseSensitivity, String tableNames, OdbcQuoteCharacter odbcquote, SQLiteLibrary sqliteLibrary, Transactio
nOptions transactionOptions, FSharpOption`1 commandTimeout)
   at Program.main(String[] argv) in /Users/Marvs/source/fsharp/FSAccountGo/src/AccountGoCLI/Program.fs:line 18

But I can successfully build the solution/project "dotnet build".

Here is my .csproj file

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <FscToolPath>/Library/Frameworks/Mono.framework/Versions/Current/bin</FscToolPath>
    <FscToolExe>fsharpc</FscToolExe>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="SQLProvider" Version="1.1.52" />
  </ItemGroup>

  <Target Name="BeforeBuild">
      <Copy SourceFiles="/Users/Marvs/.nuget/packages/sqlprovider/1.1.52/lib/netstandard2.0/" DestinationFolder="$(OutDir)" />
  </Target>
</Project>

I'm not sure if I need this line, but with without it same output.

  <Target Name="BeforeBuild">
      <Copy SourceFiles="/Users/Marvs/.nuget/packages/sqlprovider/1.1.52/lib/netstandard2.0/" DestinationFolder="$(OutDir)" />
  </Target>

But these lines seems important as I am getting other errors without it. Why I need this? This looks my project can't be run on windows.

    <FscToolPath>/Library/Frameworks/Mono.framework/Versions/Current/bin</FscToolPath>
    <FscToolExe>fsharpc</FscToolExe>

Mono is already in my PATH
PATH=/Users/Marvs/.cargo/bin:~/Library/Python/2.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/Marvs/source/go/bin:/Users/Marvs/bin:/Library/Frameworks/Mono.framework/Versions/Current/bin/

Repro steps

Please provide the steps required to reproduce the problem

  1. Create a solution and F# console project

  2. Add SQLProvider package to F# console project. "dotnet add package SQLProvider --version 1.1.52"

  3. Add these lines

open FSharp.Data.Sql

let [<Literal>] dbVendor = Common.DatabaseProviderTypes.MSSQLSERVER
let [<Literal>] connString = "Server=localhost;Database=accountgo;User Id=sa;Password=Str0ngPassword;"
let [<Literal>] indivAmount = 1000
let [<Literal>] useOptTypes  = true

type sql =
    SqlDataProvider<
        DatabaseVendor = dbVendor,
        ConnectionString = connString,
        IndividualsAmount = indivAmount,
        UseOptionTypes = useOptTypes>

[<EntryPoint>]
let main argv =
    let ctx = sql.GetDataContext()
    ctx.Dbo.Company.Individuals.``1`` |> printf "%A"
    0 // return an integer exit code
  1. Build and run "dotnet build", "dotnet run"

Expected behavior

NO Unhandled Exception: System.TypeLoadException: Could not load type

Actual behavior

Unhandled Exception

Known workarounds

Please provide a description of any known workarounds.

Related information

  • MS SQL
  • MacOS Mojave v10.14
  • Branch
  • Mono version 5.12.0
    .NET Core SDK (reflecting any global.json):
    Version: 2.1.401
    Commit: 91b1c13032

Runtime Environment:

 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.401/

Host (useful for support):
  Version: 2.1.3
  Commit:  124038c13e

.NET Core SDKs installed:
  2.0.0 [/usr/local/share/dotnet/sdk]
  2.1.4 [/usr/local/share/dotnet/sdk]
  2.1.401 [/usr/local/share/dotnet/sdk]
  • Performance information, links to performance testing scripts
@talbottmike
Copy link

You need a post build step to ensure the needed dll is present at runtime.

It took me a little bit to get it working properly in my project, but looking at issue #462 was helpful.
I think the issue(s) I had were path related (self inflicted), but I just kept tweaking it until I got the needed dll to show up in my build artifacts.

Also, in case you haven't seen it already, this is mentioned in the docs here. http://fsprojects.github.io/SQLProvider/core/netstandard.html#Microsoft-Sql-Server

@tylerhartwig
Copy link

I saw something like this with SQLProvider 1.1.52, I reverted to 1.1.49 which fixed the issue.

Could you try that version? see if we are having the same problem? I'm using Postgres, not MSSQL.

@Thorium
Copy link
Member

Thorium commented Nov 30, 2018

Try adding references to System.Data and System.Data.SqlClient

@fluxlife
Copy link

fluxlife commented Apr 23, 2019

I am having this same issue with a similar Runtime Environment as the OP, however I am using Paket.

EDIT: Actually I am receiving this error when trying to build my app

@ZergRushJoe
Copy link

ZergRushJoe commented Jul 27, 2020

@Thorium Is this an issue with system.data.sqlclinet? If so should we be putting an issue in with that repo

@Thorium
Copy link
Member

Thorium commented Jul 27, 2020

I'm not sure how well System.Data.SqlClient supports .NET Core 3.1. However, this can also be SQLProvider not finding correct dlls when doing reflection loading.

I have only Windows environment near, but this works for me:
#645 (comment)

@ZergRushJoe
Copy link

ZergRushJoe commented Jul 31, 2020

@Thorium you can get docker for windows and run Linux container builds locally. that is what I'm doing before pushing up to are linux server. Let me know if there is anything I can do to help out

@jkone27
Copy link

jkone27 commented Jun 16, 2022

was this resolved? i also got this error from macos in .fsx dotnet6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Core reference assembly loading Not a bug, but a question about usability with .NET Core. Right dlls are not in ResolutionPath.
Projects
None yet
Development

No branches or pull requests

7 participants