Providing connection string to Linq-To-Sql data provider

让人想犯罪 __ 提交于 2019-12-06 23:47:26

问题


Is there a way to provide a connection string to Linq-To-Sql data provider in F# from App.Config file.

I have tried the following just for testing:

let mutable connString = @"Data Source=PCSQLEXPRESS;Initial Catalog=NortwindDB;Integrated Security=True"
type SqlConnection = SqlDataConnection<ConnectionString = connString>

but I get an error message "This is not a constant expression or valid custom attribute value"

Thanks


回答1:


The type provider itself requires a hard-coded connection string for generating the type (in your case SqlConnection) to develop against at compile time, but, you can configure the actual connection string used at runtime like so:

type SqlConnection = SqlDataConnection<"Data Source=PCSQLEXPRESS;Initial Catalog=NortwindDB;Integrated Security=True">
let runtimeConnStr = ...
type dataContext = SqlConnection.GetDataContext(runtimeConnStr)



回答2:


Maybe using the "?ConnectionStringName" parameter will get you where you want.

http://msdn.microsoft.com/en-us/library/hh362320(v=VS.110).aspx




回答3:


You might also want to look at the following question which has a solution for this in answers F# Type Providers and Continuous Integration



来源:https://stackoverflow.com/questions/10524710/providing-connection-string-to-linq-to-sql-data-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!