Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is one supposed to use the F# SqlDataConnection TypeProvider with an App.Config file?

Tags:

app-config

f#

I am using the type expression:

type dbSchema = SqlDataConnection<ConnectionStringName="X1", ConfigFile="App.config">

This works great at compile time (I have full access to all the db types), but it fails at run time. I presume it's because the config file generated in the console application's bin directory is named something else, such as MyAppName.exe.config, and therefore the App.config file is not found.

Certainly, for an ASP.NET MVC type app that uses web.config, there's no issue because the compile and runtime config filenames are the same.

Fortunately, placing a duplicate App.config in the bin directory does remediate the problem, but is that what we are expected to do? Any thoughts?

like image 406
Clifford Ritchie Avatar asked Dec 07 '12 18:12

Clifford Ritchie


1 Answers

The description of how the type provider definition works is misleading - the value in the typedef really only matters at code/compile time, and as a default at runtime. However, as you've noted, it isn't very smart about finding the correct config file at runtime.

You can accomplish what you want by passing the connection string as a parameter to GetDataContext:

type dbSchema = SqlDataConnection<ConnectionStringName="X2">
let db = dbSchema.GetDataContext(ConfigurationManager.ConnectionStrings.["X2"].ConnectionString)

...or if you also want to make it work in F# interactive, wrap it like so:

type dbSchema = SqlDataConnection<ConnectionStringName="X2">
#if COMPILED
let db = dbSchema.GetDataContext(ConfigurationManager.ConnectionStrings.["X2"].ConnectionString)
#else
let db = dbSchema.GetDataContext()
#endif

(Note that you will need a reference to System.Configuration.)

like image 106
Overlord Zurg Avatar answered Sep 18 '22 18:09

Overlord Zurg