Search code examples
sqlsql-serverf#type-providers

Is there a way to store schema locally for F# SQL TypeProviders?


I'm making use of the FSharp.Data.SqlClient SQL type providers, and I wanted to know if there was a way to store the database schema locally.

For other type providers, like JSON or YAML, you're able to store a sample locally so that the type provider knows the "shape" of the incoming data. I was wanting to do the same for the SQL type provider.

Yes, I realize that would mean regenerating to store locally each time there is a change in the schema.


Solution

  • Different providers have different capabilities. FSharp.Data.SqlClient and SQLProvider currently cannot do this. But the (formerly built-in) FSharp.Data.TypeProviders's SqlDataConnection and SqlEntityConnection can:

    open FSharp.Data.TypeProviders
    
    type MyDb = SqlDataConnection<connectionString, 
                    LocalSchemaFile = "mydb.dbml",
                    ForceUpdate = false>
    

    This will save the schema in the file mydb.dbml, and subsequent compiles will load it from there instead of connecting to the database. If you need to update the file because the schema has changed, just set ForceUpdate to true, recompile, then set it back to false. You can commit the dbml file and people will be able to compile the project without access to the database.