mysql - 从 App.config F# AppSettings 提供程序获取和使用连接字符串

标签 mysql f# app-config type-providers

我已经在网上搜索了几个小时,但还是做不到,所以我希望有人能帮我解决这个问题。

我曾尝试直接使用 App.Config,但总是返回 null。所以我求助于 AppSetting typerprovider .这确实返回了预期的字符串,但我有一个问题,到目前为止(通过谷歌)没有人有答案。

open System.Linq
open FSharp.Data.Sql
open MySql.Data
open FSharp.Configuration 
type Settings = AppSettings<"App.config">

let c = Settings.ConnectionStrings.RaspberryPi 

[<Literal>]
let connection = c //quick attempt to turn c into a literal.

type sql = SqlDataProvider< ConnectionString = c,  ResolutionPath = @"C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.7\Assemblies\v4.5\"  >

我收到消息“这不是有效的常量表达式或自定义属性值。”

虽然我能够打印出 Appsettings 提供程序获得的设置,但我似乎无法使用它来连接到数据库。

这是 App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="raspberryPi" providerName="FSharp.Data.SqlProvider" connectionString="Server=SomeDB.rds.amazonaws.com:3306;Database=SomeDB;Uid=SomeUser;Pwd=APassword" />
  </connectionStrings>
</configuration>

我已经求助于 typeprovider,因为我之前使用 System.Configuration 读取文件的方法无法正常工作,即使我对输出文件夹进行了快速处理并确保所有配置文件都已更新。

请注意我也在使用 MySQL 类型提供程序!

虽然我可以直接在代码中使用字符串,但我的理解是这对于生产代码来说是个糟糕的主意。

如有任何帮助,我们将不胜感激 - 我不介意承认今天这已经耗费了几个小时,这让我非常惊讶。

注意 - 可能会帮助其他人:我也看过 this answer , this articlethis one from the MS Forums

最佳答案

不幸的是,这行不通。连接字符串由读/写静态属性 (Settings.ConnectionStrings.RaspberryPi) 公开,不能将其视为常量/文字(按设计)。

有人可以编写一个类型提供程序,通过将连接字符串公开为文字静态字段而不是读/写属性来执行您想要的操作,但这与更新应用程序设置不兼容,这似乎是一个现有供应商的目标。或许您可以考虑向 AppSettings 项目提交功能请求,以启用一个单独的模式来按照您的意愿运行。

关于mysql - 从 App.config F# AppSettings 提供程序获取和使用连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32462826/

相关文章:

mysql - Mysql函数耗时太长

f# - 如何在 Rx 中进行模式匹配。 Where 和 Select 在单个运算符中?

arrays - 加载具有固定数据模式的数组

.net - 引用项目中的 app.config 会发生什么

vb.net - 在运行时从主应用程序中的类库更改连接字符串

php - 查询没有更新

mysql - 用户代码未处理 EvaluateException 找不到列 [年份]

mysql - 这是一个有效的 MySQL 查询吗?

c# - 我如何在 C# 中获得特定运算符的函数?

c# - 切换不同的app.config