SQLProvider

SQL Provider Static Parameters

Global parameters

These are the "common" parameters used by all SqlProviders.

All static parameters must be known at compile time. For strings, this can be achieved by adding the [<Literal>] attribute if you are not passing it inline.

ConnectionString

This is the connection string commonly used to connect to a database server instance. Please review the documentation on your desired database type to learn more.

[<Literal>]
let sqliteConnectionString =
    "Data Source=" + __SOURCE_DIRECTORY__ + @"\northwindEF.db;Version=3"

ConnectionStringName

Instead of storing the connection string in the source code / fsx script, you can store values in the App.config file:

<connectionStrings>  
  <add name="MyConnectionString"   
   providerName="System.Data.ProviderName"   
   connectionString="Valid Connection String;" />  
</connectionStrings>

Another usually easier option is to give a runtime connection string as a parameter for the .GetDataContext(...) method.

In your source file:

let connexStringName = "MyConnectionString"

DatabaseVendor

Select enumeration from Common.DatabaseProviderTypes to specify which database type the provider will be connecting to.

[<Literal>]
let dbVendor = Common.DatabaseProviderTypes.SQLITE

ResolutionPath

A third-party driver is required when using database vendors other than SQL Server, Access and ODBC. This parameter should point to an absolute or relative directory where the relevant assemblies are located. Please review at the database vendor-specific page for more details.

[<Literal>]
let resolutionPath =
    __SOURCE_DIRECTORY__ + @"..\..\..\files\sqlite"

The resolution path(s) (as can be semicolon separated if any) should point to the database driver files and their reference assemblies) that work on design-time. So, depending on your IDE, you probably want there .NET Standard 2.0 (or 2.1) versions and not the latest .NET runtime, even when you'd target your final product to the latest .NET.

Note on .NET 5 PublishSingleFile and ResolutionPath

If you are publishing your app using .NET 5's PublishSingleFile mode, the driver will be loaded from the bundle itself rather than from a separate file on the drive. The ResolutionPath parameter will not work for the published app, nor will the automatic assembly resolution implemented within SQLProvider.

SQLProvider attempts to load the assembly from the AppDomain in such a case. This means that your driver's assembly must be loaded by your application for SQLProvider to find it. To do so, use the types of your driver before calling the .GetDataContext(...) method, such as in this example, using MySqlConnector. The specific type you refer to does not matter.

typeof<MySqlConnector.Logging.MySqlConnectorLogLevel>.Assembly |> ignore
let ctx = sqlType.GetDataContext()

IndividualsAmount

Number of instances to retrieve using the individuals feature. Default is 1000.

let indivAmt = 500

UseOptionTypes

If set to FSharp.Data.Sql.Common.NullableColumnType.OPTION, all nullable fields will be represented by F# option types. If NO_OPTION, nullable fields will be represented by the default value of the column type - this is important because the provider will return 0 instead of null, which might cause problems in some scenarios.

The third option is VALUE_OPTION, where nullable fields are represented by ValueOption struct.

[<Literal>]
let useOptionTypes = FSharp.Data.Sql.Common.NullableColumnType.OPTION

ContextSchemaPath

Defining ContextSchemaPath and placing a file with schema information according to the definition enables offline mode that can be useful when the database is unavailable or slow to connect or access. Schema information file can be generated by calling design-time method SaveContextSchema under Design Time Commands:

ctx.``Design Time Commands``.SaveContextSchema

This method doesn't affect runtime execution. Since SQLProvider loads schema information lazily, calling SaveContextSchema only saves the portion of the database schema sufficient to compile queries referenced in the scope of the current solution or script. Therefore, it is recommended that it be executed after the successful build of the whole solution. Type the method name with parentheses. If you then type a dot (.), you should see a tooltip with information about when the schema was last saved. Once the schema is saved, the outcome of the method execution is stored in memory so the file will not be overwritten. In case the database schema changes and the schema file must be updated, remove the outdated file, reload the solution and retype or uncomment a call to SaveContextSchema to regenerate the schema file.

There is a tool method FSharp.Data.Sql.Common.OfflineTools.mergeCacheFiles to merge multiple files together.

[<Literal>]
let contextSchemaPath =
    __SOURCE_DIRECTORY__ + @".\sqlite.schema"

Platform Considerations

MSSQL

TableNames to filter the amount of tables.

Oracle

TableNames to filter the number of tables and an Owner.

Owner (Used by Oracle, MySQL and PostgreSQL)

This has different meanings when running queries against different database vendors.

For PostgreSQL, this sets the schema name to which the target tables belong. It can also be a list separated by spaces, newlines, commas or semicolons.

For MySQL, this sets the database name (Or schema name, which is the same thing for MySQL). It can also be a list separated by spaces, newlines, commas or semicolons.

For Oracle, this sets the owner of the scheme.

SQLite

The additional SQLiteLibrary parameter can be used to specify which SQLite library to load.

PostgreSQL

There are no extra parameters.

MySQL

There are no extra parameters.

ODBC

There are no extra parameters.

Example

It is recommended to use named static parameters in your type provider definition so

type sql = SqlDataProvider<
            ConnectionString = sqliteConnectionString,
            DatabaseVendor = dbVendor,
            ResolutionPath = resolutionPath,
            UseOptionTypes = useOptionTypes
          >

SQL Provider Data Context Parameters

Besides the static parameters the .GetDataContext(...) method has optional parameters:

Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
namespace FSharp.Data.Sql
Multiple items
type LiteralAttribute = inherit Attribute new: unit -> LiteralAttribute

--------------------
new: unit -> LiteralAttribute
[<Literal>] val connectionString: string = "Data Source=C:\git\SQLProvider\docs\content\core/../../../tests/SqlProvider.Tests/scripts/northwindEF.db;Version=3"
[<Literal>] val resolutionPath: string = "C:\git\SQLProvider\docs\content\core/../../../tests/SqlProvider.Tests/libs"
type sqlType = obj
type SqlDataProvider
<summary>Typed representation of a database</summary> <param name='ConnectionString'>The connection string for the SQL database</param> <param name='ConnectionStringName'>The connection string name to select from a configuration file</param> <param name='DatabaseVendor'> The target database vendor</param> <param name='IndividualsAmount'>The amount of sample entities to project into the type system for each SQL entity type. Default 50. Note GDPR/PII regulations if using individuals with ContextSchemaPath.</param> <param name='UseOptionTypes'>If set, F# option types will be used in place of nullable database columns. If not, you will always receive the default value of the column's type even if it is null in the database.</param> <param name='ResolutionPath'>The location to look for dynamically loaded assemblies containing database vendor specific connections and custom types. Types used in desing-time: If no better clue, prefer .NET Standard 2.0 versions. Semicolon to separate multiple.</param> <param name='Owner'>Oracle: The owner of the schema for this provider to resolve. PostgreSQL: A list of schemas to resolve, separated by spaces, newlines, commas, or semicolons.</param> <param name='CaseSensitivityChange'>Should we do ToUpper or ToLower when generating table names?</param> <param name='TableNames'>Comma separated table names list to limit a number of tables in big instances. The names can have '%' sign to handle it as in the 'LIKE' query (Oracle and MSSQL Only)</param> <param name='ContextSchemaPath'>The location of the context schema previously saved with SaveContextSchema. When not empty, will be used to populate the database schema instead of retrieving it from then database.</param> <param name='OdbcQuote'>Odbc quote characters: Quote characters for the table and column names: `alias`, [alias]</param> <param name='SQLiteLibrary'>Use System.Data.SQLite or Mono.Data.SQLite or select automatically (SQLite only)</param> <param name='SsdtPath'>A path to an SSDT .dacpac file.'</param>
namespace FSharp.Data.Sql.Common
[<Struct>] type DatabaseProviderTypes = | MSSQLSERVER = 0 | SQLITE = 1 | POSTGRESQL = 2 | MYSQL = 3 | ORACLE = 4 | MSACCESS = 5 | ODBC = 6 | FIREBIRD = 7 | MSSQLSERVER_DYNAMIC = 8 | MSSQLSERVER_SSDT = 9 | DUCKDB = 10
Common.DatabaseProviderTypes.SQLITE: Common.DatabaseProviderTypes = 1
[<Literal>] val sqliteConnectionString: string = "Data Source=C:\git\SQLProvider\docs\content\core\northwindEF.db;Version=3"
val connexStringName: string
[<Literal>] val dbVendor: Common.DatabaseProviderTypes = 1
[<Literal>] val resolutionPath: string = "C:\git\SQLProvider\docs\content\core..\..\..\files\sqlite"
val typeof<'T> : System.Type
val ignore: value: 'T -> unit
val ctx: obj
val indivAmt: int
[<Literal>] val useOptionTypes: Common.NullableColumnType = 1
[<Struct>] type NullableColumnType = | NO_OPTION = 0 | OPTION = 1 | VALUE_OPTION = 2
Common.NullableColumnType.OPTION: Common.NullableColumnType = 1
<summary> Option types are Option&lt;_&gt;. (Old true.) </summary>
[<Literal>] val contextSchemaPath: string = "C:\git\SQLProvider\docs\content\core.\sqlite.schema"
type sql = obj

Type something to start searching.