Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in F#, when is a "constant string expression" not a "constant string expression"

New to F#, so I'm still kind of getting used to "type inference". I am trying to use EntityFramework in F# and when I try to build the connection, I get the error that my connection string (that's read from the app.config) is "not a valid constant expression or custom attribute value". If I hard-code the connection string it's fine, just like you'd expect.

open FSharp.Configuration
type Settings = AppSettings<"app.config">
let ConnStr = Settings.ConnectionStrings.Model
type private Connection = SqlEntityConnection<ConnectionString=ConnStr, Pluralize = true>

what am I doing wrong? is there a different, "F# way", of doing this?

like image 578
mrjawright Avatar asked Jun 07 '16 16:06

mrjawright


People also ask

What is 1 C equal to in Fahrenheit?

Answer: 1° Celsius is equivalent to 33.8° Fahrenheit.

What is the difference between 1 degree Celsius and 1 degree Fahrenheit?

In the Celsius scale there are 100 degrees between the freezing point and the boiling point of water compared to 180 degrees in the Fahrenheit scale. This means that 1 °C = 1.8 °F (check the section about temperature differences below).

What temperature in Fahrenheit is 50 C?

Answer: 50° Celsius is equal to 122° Fahrenheit.


1 Answers

SqlEntityConnection is a Type Provider. Type Providers work at compile time (think of them as compiler plugins; or if you're into Lisp, think of them as a poor man's macros). Therefore, all Type Provider arguments need to be known at compile time.

Now, ask the question: is ConnStr known at compile time?
No, of course it isn't, because you want to lift it from your config file.

The way you're supposed to work with this is:

  1. Hard code the connection string, point it to a database that is available at compile time. This will give the type provider an opportunity to look at the database at compile time, and generate all the types from it.
  2. When you call Connection.GetDataContext, give it the runtime connection string as argument. This will tell it to connect to whatever database is actually specified in your config, not the one you hard coded at compile time.
like image 56
Fyodor Soikin Avatar answered Oct 17 '22 18:10

Fyodor Soikin