从App.config F#AppSettings提供程序获取和使用连接字符串 [英] Get and use connection string from App.config F# AppSettings provider

查看:86
本文介绍了从App.config F#AppSettings提供程序获取和使用连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网上搜了好几个小时了,但是做不到,所以我希望有人可以帮忙解决这个问题.

I've scoured the net for hours and can't do it, so I'm hoping someone can help me with this one.

我尝试直接使用App.Config,但始终返回null.因此,我转向了 AppSetting typerprovider .这确实返回了预期的字符串,但是我有一个问题,到目前为止,没有人(通过Google)能找到答案.

I've tried to use App.Config directly but constantly had null return. So I've turned to an AppSetting typerprovider. This does return the string expected but I have a problem that no-one so far (via google) has an answer for.

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\"  >

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

I get the message that "This is not a valid constant expression or custom attribute value."

虽然我可以打印出Appsettings提供程序所获得的设置,但似乎无法使用它来连接数据库.

Whilst I'm able to print out the setting the Appsettings provider obtains, it seems I can't use it to connect to a database.

这是App.config:

This is the 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读取文件的较早方法,即使我突击输出文件夹并确保所有配置文件都已更新,该方法也无法正常工作.

I've turned to the typeprovider as my earlier approach to using System.Configuration to read the file refused to work right, even when I blitzed the output folders and made sure that all the config files were updated.

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

Please note I'm using a MySQL type provider as well!

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

Whilst I could use the string directly in the code, my understanding is this is a terrible idea for production code.

我们将非常感激收到任何帮助-我不介意承认今天这已经烧了好几个小时,令我感到惊讶的是.

Any help would be very gratefully received - I don't mind admitting that this has burned hours today, to my great surprise.

注意-可能会对其他人有所帮助:我也查看了这答案本文

Note - may help others: I've also looked at this answer, this article and this one from the MS Forums

推荐答案

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

That won't work, unfortunately. The connection string is being exposed by a read/write static property (Settings.ConnectionStrings.RaspberryPi), which can't be treated as a constant/literal (by design).

有人可能会编写类型提供程序,通过将连接字符串显示为文字静态字段(而不是读/写属性)来实现所需的功能,但这与更新应用程序设置不兼容,这似乎是一种现有提供者的目标.也许您可以考虑向AppSettings项目提交功能请求,以启用可以按照您想要的方式运行的单独模式.

It would be possible for someone to write a type provider that does what you want by exposing the connection strings as literal static fields instead of read/write properties, but that's not compatible with updating the application settings, which seems to be a goal of the existing provider. Perhaps you could consider submitting a feature request to the AppSettings project to enable a separate mode that would behave how you want.

这篇关于从App.config F#AppSettings提供程序获取和使用连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆