fsprojects / FSharp.Data.SqlClient

A set of F# Type Providers for statically typed access to MS SQL database
http://fsprojects.github.io/FSharp.Data.SqlClient/
Other
205 stars 71 forks source link

Sql Errors not bubbling up - System.TypeInitializationException #268

Open jkone27 opened 7 years ago

jkone27 commented 7 years ago

Description

When executing two delete statements in the wrong order, e.g. violating the foreing key constraint, so for example, deleting headers table before rows table, the type provider does not bubble up the sql exception at runtime, but gives instead a type initialization exception which does not allow to properly track the issue, without executing the command separately.

Repro steps

Please provide the steps required to reproduce the problem

(* connectionString is one of SqlServer 2014, 
   whereas devConnectionString is of SqlServer 2008 *)

[<Literal>]
let deleteMovement =
   @"DELETE
    FROM Movements
    WHERE ID= @id
    "
type DeleteMovement = SqlCommandProvider<deleteMovement, connectionString>

let delMovement id =
     use cmd = new DeleteMovement(devConnectionString) 
     in printfn "%s" (cmd.ToTraceString id) ;
     cmd.Execute(id)

[<Literal>]
let deleteMovementRows =
   @"DELETE
    FROM MovementRows
    WHERE movement_ID = @MovementId
    "

type DeleteMovementRows = SqlCommandProvider<deleteMovementRows, connectionString>

let delMovementRows movementId =
    use cmd = new DeleteMovementRows(devConnectionString) 
    in printfn "%s" (cmd.ToTraceString movementId) ;
    cmd.Execute(movementId) 

let deleteMovementsAndRows ids =
    ids |> Seq.iter(fun x -> 
            delMovement x |> printfn "deleted movements %A";
            delMovementRows x |> printfn "deletes movements rows %A"
        )

Expected behavior

SqlCommandProvider should raise an exception at runtime, containing the appropriate error with constraint violation on foreign key, or whatever constraint violation happens.

Actual behavior

An unhandled exception of type System.TypeInitializationException occurred in [...].exe

Additional information: The type initializer for <StartupCode$MessageGenerator>.$Program threw an exception.

Known workarounds

No workarounds at this time. just grab sql and execute it against database in the same order of execution, to get the correct error message that should be displayed

Related information

dsevastianov commented 7 years ago

Is 2008 for execution required to repro the issue?