SqlDataConnection Type Provider (F#)

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 Parameter

Description

ConnectionString : string

The 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 : string

The name of the connection string for the database connection in the configuration file.

?LocalSchemaFile : string

The 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 : bool

Requires 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 : bool

If 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 : bool

If true, exposes database views in the generated types. The default value is true.

?Functions : bool

If true, exposes database functions in the generated types. The default value is true.

?ConfigFile : string

The name of the configuration file that's used for connection strings. The default value is app.config or web.config.

?DataDirectory : string

The name of the data directory, used to replace |DataDirectory| in connection strings. The default value is the project or script directory.

?ResolutionFolder : string

A folder to be used to resolve relative file paths at compile time. The default value is the folder that contains the project or script.

?StoredProcedures

If true, exposes stored procedures in the generated types. The default value is true.

?Timeout : int

The timeout, in seconds, for SQLMetal.exe to use when it connects to the database. The default value is zero, which means no timeout.

?ContextTypeName : string

The name of the container type that you use to access all the generated types.

?Serializable : bool

If true, generated types are serializable. The default value is false.

Remarks

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 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.

Type

Description

MyDb

The 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.ServiceTypes

Contains the embedded full types and simplified types for the database.

MyDb.ServiceTypes.*

The embedded types generated by SqlMetal.exe.

MyDb.ServiceTypes.DataContextTypeName

The data context type, inherited from DataContext.

MyDb.ServiceTypes.SimpleDataContextTypes. DataContextTypeName

Contains 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 ISingleResult<T>.

Contains one property for each property of the full context type. The properties return Table<TEntity>.

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

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

Warning

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

Platforms

Windows 8, Windows 7, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 4.0

See Also

Tasks

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

Reference

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

SqlMetal.exe (Code Generation Tool)