简体   繁体   English

使用app.config中的connectionstring和FSharp.Data.SqlClient

[英]Using connectionstring from app.config with FSharp.Data.SqlClient

I'm using FSharp.Data.SqlClient and trying to move my connectionString from a [<Literal>] to the app.config. 我正在使用FSharp.Data.SqlClient并尝试将我的connectionString从[<Literal>]到app.config。

My app.config looks like this 我的app.config看起来像这样

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
</configuration>

And my SqlCommandProvider looks like the below, which should be correct according to http://fsprojects.github.io/FSharp.Data.SqlClient/configuration%20and%20input.html 我的SqlCommandProvider如下所示,根据http://fsprojects.github.io/FSharp.Data.SqlClient/configuration%20and%20input.html ,这应该是正确的

new SqlCommandProvider<"SELECT ...", 
       ConnectionStringOrName = "name=DefaultConnection", 
       SingleRow = true, 
       AllParametersOptional = true>(??????)

Now the question is. 现在的问题是。 What goes in the last part, the ?????? 什么在最后一部分, ?????? part. 部分。

I tried "name=DefaultConnection" but it gives me a runtime error with name being unsupported. 我尝试了"name=DefaultConnection"但它给了我一个名称不受支持的运行时错误。

I can't seem to find any documentation explaining what goes there. 我似乎找不到任何文件来解释那里发生了什么。

Update 更新

Instaed of fixnig the issue I found this workaround. Instaed of fixnig我发现了这个问题的解决方法。 https://fsprojects.github.io/FSharp.Configuration/ https://fsprojects.github.io/FSharp.Configuration/

I don't get the purpose of ConnectionStringOrName if you have to supply the connection string anyway. 如果你必须提供连接字符串,我不会得到ConnectionStringOrName的目的。 Also why do you have to specify it twice. 另外你为什么要指定它两次。 Makes very little sense to me :( 对我来说没什么意义:(

When using type providers, you often need two separate data sources: 使用类型提供程序时,通常需要两个单独的数据源:

  • Compile-time one that is used when you are editing the code and compiling the code. 编译时编译代码和编译代码时使用的编译时间 The type provider uses this connection or data source to infer the schema of the data - in case of SQL provider, this is connection to the database that is used to check that all your column names exist etc. 类型提供程序使用此连接或数据源来推断数据的模式 - 在SQL提供程序的情况下,这是与用于检查所有列名称是否存在的数据库的连接等。

  • Run-time one is used when you actually run the program after it is deployed somewhere. 在部署某个地方后实际运行程序时,将使用运行时一个 This is where you'll read the actual data from. 您可以在这里阅读实际数据。

The reason why you need two is that the run-time data source may be determined at runtime and it may not be accessible at compile-time (you typically have access to a dev database, but not to production database). 您需要两个的原因是运行时数据源可能在运行时确定,并且可能无法在编译时访问(您通常可以访问开发数据库,​​但不能访问生产数据库)。 The compile-time connection needs to be a constant, so that the provider can use it (when compiling your code) without running any part of your code. 编译时连接需要是一个常量,以便提供者可以使用它(在编译代码时)而不运行代码的任何部分。

In case of the SQL command provider: 如果是SQL命令提供程序:

type SelectCmd = SqlCommandProvider<"SELECT ...", 
   ConnectionStringOrName = "name=DefaultConnection", 
   SingleRow = true, 
   AllParametersOptional = true>

let cmd = new SelectCmd(??????)
  • "name=DefaultConnection" tells the provider to use an app.config key at compile-time "name=DefaultConnection"告诉提供程序在编译时使用app.config
  • ????? is where you need to specify the run-time connection string 是您需要指定运行时连接字符串的位置

To read the connection string from app.config , you can use the standard .NET methods like using ConfigurationManager : 要从app.config读取连接字符串,您可以使用标准.NET方法, 例如使用ConfigurationManager

open System.Configuration

let conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
let cmd = new SelectCmd(conn)

Something like this would not work when passing the connection string to the SqlCommandProvider , because this needs to run some code to read the string and that's only possible at runtime. 将连接字符串传递给SqlCommandProvider ,这样的东西不起作用,因为这需要运行一些代码来读取字符串,而这只能在运行时使用。 That's why the SQL command provider has a handy option to specify name=DefaultConnection as a special string. 这就是为什么SQL命令提供程序有一个方便的选项来指定name=DefaultConnection作为特殊字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM