Table of contents
Collapse the table of content
Expand the table of content

SqlDataConnection Type Provider (F#)

Den Delimarsky|Last Updated: 7/25/2016
1 Contributor

Provides the types to access a SQL database.

Namespace/Module Path: Microsoft.FSharp.Data.TypeProviders

Assembly: FSharp.Data.TypeProviders (in FSharp.Data.TypeProviders.dll)


type SqlDataConnection<?ConnectionString : string,
                       ?ConnectionStringName : string,
                       ?LocalSchemaFile : string,
                       ?ForceUpdate : bool,
                       ?Pluralize : bool,
                       ?Views : bool,
                       ?Functions : bool,
                       ?ConfigFile : string,
                       ?DataDirectory : string,
                       ?ResolutionFolder : string,
                       ?StoredProcedures : bool,
                       ?Timeout : int,
                       ?ContextTypeName : string,
                       ?Serializable : bool>

Static Type Parameters

Type ParameterDescription
ConnectionString : stringThe connection string for the database connection. If you are using Visual Studio, a connection string can be found in database properties in the Server Explorer window.
ConnectionStringName : stringThe name of the connection string for the database connection in the configuration file.
?LocalSchemaFile : stringThe path to a .dbml file that contains the schema. The default value is no local schema file. This option is frequently used with ForceUpdate.
?ForceUpdate : boolRequires that a direct connection to the database be available at design time and forces the the local schema file to be refreshed. The default value is true. When ForceUpdate is false, the provider reacts to changes in the LocalSchemaFile.
?Pluralize : boolIf true, uses plural forms for the generated type names. The generated plural forms are based on English language rules. The default value is false.
?Views : boolIf true, exposes database views in the generated types. The default value is true.
?Functions : boolIf true, exposes database functions in the generated types. The default value is true.
?ConfigFile : stringThe name of the configuration file that's used for connection strings. The default value is app.config or web.config.
?DataDirectory : stringThe name of the data directory, used to replace |DataDirectory| in connection strings. The default value is the project or script directory.
?ResolutionFolder : stringA folder to be used to resolve relative file paths at compile time. The default value is the folder that contains the project or script.
?StoredProceduresIf true, exposes stored procedures in the generated types. The default value is true.
?Timeout : intThe timeout, in seconds, for SQLMetal.exe to use when it connects to the database. The default value is zero, which means no timeout.
?ContextTypeName : stringThe name of the container type that you use to access all the generated types.
?Serializable : boolIf true, generated types are serializable. The default value is false.


For a walkthrough that shows how to use this type provider, see Walkthrough: Accessing a SQL Database by Using Type Providers (F#).

The SqlDataConnection type provider is meant to be used when you have a direct connection to a SQL database and want to generate types that represent the data in the database. If you don’t want to use a live connection to a database during development, you can use the DbmlFile Type Provider (F#).

The top-level type generated by a simple call to the type provider contains a nested type called ServiceTypes, under which the types generated from the database schema appear. The top-level type itself contains two overloads of the method GetDataContext, one that takes no parameters and one that take a connection string as a parameter. Two data contexts are available, the full view generated by SqlMetal.exe and a simplified view that removes utility methods but preserves properties for data fields. This top-level GetDataContext gets a simplified view of the types. The GetDataContext method returns a type derived from System.Data.Linq.DataContext, and the name of the type is either given by the static type parameter ContextTypeName, or is determined by SQLMetal.exe if the static parameter is not provided. In addition to the base class methods, the data context type contains the tables in the database.

The following table summarizes the types provided by the type provider for an expression of the form:

type MyDb = SqlDataConnection<connectionString>

In the following table, DataContextTypeName is a placeholder for the name of the data context type for the provider, and * indicates all the types in the namespace.

MyDbThe overall container type.

Contains a method GetDataContext that returns a simplified view of the data context. The method returns a new instance of MyDB.ServiceTypes.SimpleDataContextTypes.DataContextTypeName. The version with the connectionString parameter may be used when the connection string is determined at runtime.
MyDb.ServiceTypesContains the embedded full types and simplified types for the database.
MyDb.ServiceTypes.*The embedded types generated by SqlMetal.exe.
MyDb.ServiceTypes.DataContextTypeNameThe data context type, inherited from System.Data.Linq.DataContext.
MyDb.ServiceTypes.SimpleDataContextTypes. DataContextTypeNameContains one method for each method on the full context type, including stored procedures and functions, if the options for these have been selected. The methods return System.Data.Linq.ISingleResult.

Contains one property for each property of the full context type. The properties return System.Data.Linq.Table.

The property Connection gets the database connection as an instance of System.Data.Common.DbConnection.

The property DataContext gets the full data context, of type System.Data.Linq.DataContext. This is the base type of the DataContextTypeName type generated by the type provider.

To improve performance of read-only operations, set the System.Data.Linq.DataContext.ObjectTrackingEnabled property on the System.Data.Linq.DataContext object to false.


Windows 8.1, Windows 7, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 4.0

See Also

Microsoft.FSharp.Data.TypeProviders Namespace (F#)

Walkthrough: Accessing a SQL Database by Using Type Providers (F#)

SqlMetal.exe (Code Generation Tool)

© 2017 Microsoft