When using type providers, you often need two separate data sources:
Compilation time , which is used when editing code and compiling code. The type provider uses this connection or data source to output the data schema β in the case of the SQL provider, it is a database connection that is used to check for all column names, etc.
Runtime is used when you actually run the program after deploying it somewhere. Here you will read the actual data.
The reason you need two is because the runtime source at runtime can be determined at runtime, and it may not be available at compile time (usually you have access to the dev database, but not to the production database data). The compile-time connection must be permanent so that the provider can use it (when compiling your code) without using any part of your code.
In the case of the SQL command provider:
type SelectCmd = SqlCommandProvider<"SELECT ...", ConnectionStringOrName = "name=DefaultConnection", SingleRow = true, AllParametersOptional = true> let cmd = new SelectCmd(??????)
"name=DefaultConnection" tells the provider to use the app.config key at compile time????? is where you need to specify the runtime connection string
To read the connection string from app.config , you can use standard .NET methods, for example, using ConfigurationManager :
open System.Configuration let conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; let cmd = new SelectCmd(conn)
Something like this will not work when passing the connection string to SqlCommandProvider , because for this you need to run some code to read the string and this is only possible at runtime. This is why the SQL command provider has a convenient option to specify name=DefaultConnection as a special string.
source share