Search code examples
sql-serverf#type-providers

Unable to read dates with F# TypeProvider


I'm trying to get a F# console application that queries a MS SQL SERVER running in Docker. I'm using the SQLProvider to run a select statement against the DB, in the beginning only pulling an int ID. It works fine in Windows, but not in a Docker container. It fails with the error message:

 System.ArgumentOutOfRangeException: No mapping exists from SqlDbType Date to a known DbType.

Parameter name: SqlDbType

I've tried both the mono:4.2.3.4 and teh fsharp/fsharp Docker images. I'm also pulling the FSharp.Core library with me.

Any help would be appreciated. Thanks

Edit: The Docker container runs on Linux, with mono version 4.22.2.

This is my query:

type private connection = SqlDataProvider<ConnectionString="CONNECTIONSTRING",
                                      DatabaseVendor=Common.DatabaseProviderTypes.MSSQLSERVER,
                                      UseOptionTypes=true>

let private ctx = connection.GetDataContext()

let private application = "E-Mail an PH - eFeedback"

let history =
    query {
        for eh in ctx.Dbo.ActionHistoryEmail do
        join gah in ctx.Dbo.GenericActionHistory on (eh.Id = gah.Id)
        where (eh.Application = application)
        select gah.Id
    }
    |> Seq.toList

The stacktrace:

System.TypeInitializationException: The type initializer for '<StartupCode$Company-EFeedbackExport>.$Repository' threw an exception. ---> System.ArgumentOutOfRangeException: No mapping exists from SqlDbType Date     to a known DbType.
Parameter name: SqlDbType
  at System.Data.SqlClient.SqlParameter.SetSqlDbType (SqlDbType type) <0x411a3080 + 0x00afb> in <filename unknown>:0
  at System.Data.SqlClient.SqlParameter.set_SqlDbType (SqlDbType value) <0x411a3040 + 0x00017> in <filename unknown>:0

  at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlParameter:set_SqlDbType (System.Data.SqlDbType)
  at FSharp.Data.Sql.Providers.MSSqlServer.getDbType@27 (Int32 providerType) <0x411a17c0 + 0x0008b> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServer+mappings@38.GenerateNext (IEnumerable`1& next) <0x411999f0 + 0x007bb> in <filename unknown>:0
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].MoveNextImpl () <0x41199790 + 0x00069> in <filename unknown>:0
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].System-Collections-IEnumerator-MoveNext () <0x41199760 + 0x00017> in <filename unknown>:0
  at Microsoft.FSharp.Collections.SeqModule.ToList[T] (IEnumerable`1 source) <0x411991c0 + 0x001ea> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServer.createTypeMappings (IDbConnection con) <0x411761d0 + 0x00097> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServerProvider.FSharp-Data-Sql-Common-ISqlProvider-CreateTypeMappings (IDbConnection con) <0x411761a0 + 0x00013> in <filename unknown>:0
  at <StartupCode$FSharp-Data-SqlProvider>.$SqlRuntime.DataContext.f@1-29 (System.String runtimeAssembly, System.String resolutionPath, System.String[] referencedAssemblies, DatabaseProviderTypes providerType,     System.String owner, FSharp.Data.Sql.Runtime.SqlDataContext this, Microsoft.FSharp.Core.Unit unitVar0) <0x401883d0 + 0x00109> in <filename unknown>:0
  at FSharp.Data.Sql.Runtime.SqlDataContext..ctor (System.String typeName, System.String connectionString, DatabaseProviderTypes providerType, System.String resolutionPath, System.String[] referencedAssemblies,     System.String runtimeAssembly, System.String owner, CaseSensitivityChange caseSensitivity) <0x401880b0 + 0x00153> in <filename unknown>:0
  at <StartupCode$Company-EFeedbackExport>.$Repository..cctor () <0x40187650 + 0x002ab> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Program.main (System.String[] args) <0x4016de00 + 0x00087> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for '<StartupCode$Company-EFeedbackExport>.$Repository' threw an exception. ---> System.ArgumentOutOfRangeException: No     mapping exists from SqlDbType Date to a known DbType.
Parameter name: SqlDbType
  at System.Data.SqlClient.SqlParameter.SetSqlDbType (SqlDbType type) <0x411a3080 + 0x00afb> in <filename unknown>:0
  at System.Data.SqlClient.SqlParameter.set_SqlDbType (SqlDbType value) <0x411a3040 + 0x00017> in <filename unknown>:0

  at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlParameter:set_SqlDbType (System.Data.SqlDbType)
  at FSharp.Data.Sql.Providers.MSSqlServer.getDbType@27 (Int32 providerType) <0x411a17c0 + 0x0008b> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServer+mappings@38.GenerateNext (IEnumerable`1& next) <0x411999f0 + 0x007bb> in <filename unknown>:0
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].MoveNextImpl () <0x41199790 + 0x00069> in <filename unknown>:0
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].System-Collections-IEnumerator-MoveNext () <0x41199760 + 0x00017> in <filename unknown>:0
  at Microsoft.FSharp.Collections.SeqModule.ToList[T] (IEnumerable`1 source) <0x411991c0 + 0x001ea> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServer.createTypeMappings (IDbConnection con) <0x411761d0 + 0x00097> in <filename unknown>:0
  at FSharp.Data.Sql.Providers.MSSqlServerProvider.FSharp-Data-Sql-Common-ISqlProvider-CreateTypeMappings (IDbConnection con) <0x411761a0 + 0x00013> in <filename unknown>:0
  at <StartupCode$FSharp-Data-SqlProvider>.$SqlRuntime.DataContext.f@1-29 (System.String runtimeAssembly, System.String resolutionPath, System.String[] referencedAssemblies, DatabaseProviderTypes providerType,     System.String owner, FSharp.Data.Sql.Runtime.SqlDataContext this, Microsoft.FSharp.Core.Unit unitVar0) <0x401883d0 + 0x00109> in <filename unknown>:0
  at FSharp.Data.Sql.Runtime.SqlDataContext..ctor (System.String typeName, System.String connectionString, DatabaseProviderTypes providerType, System.String resolutionPath, System.String[] referencedAssemblies,     System.String runtimeAssembly, System.String owner, CaseSensitivityChange caseSensitivity) <0x401880b0 + 0x00153> in <filename unknown>:0
  at <StartupCode$Company-EFeedbackExport>.$Repository..cctor () <0x40187650 + 0x002ab> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Program.main (System.String[] args) <0x4016de00 + 0x00087> in <filename unknown>:0

Solution

  • .NET doesn't have a Date type yet. In standard .NET Date is mapped to DateTime. Mono's SqlParameter performs this mapping in three places. Two of them map Date and DateTime to the same type. The third though doesn't map date. I suspect that the call stack shows the exception is thrown inside SetSqlDbType.

    Please post the exception's call stack in the question

    It seems a similar Mono bug about DateTime2 was filed in 2014. This was fixed but Date is still missing