类中 SQL 的 F# 类型提供程序 [英] F# Type Provider for SQL in a class

查看:15
本文介绍了类中 SQL 的 F# 类型提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于 Azure Worker 角色的 F#.我希望该类将连接字符串 a 作为参数.我用

I am writing a F# to be used with Azure Worker role. I want the class to have the connection string a as a parameter. I create a db connection with

type dbSchema = SqlDataConnection<"...">
let db = dbSchema.GetDataContext()

但是 dbSchema 是一种类型,因此它不能嵌入到我的类中(另一种类型).我可以创建两个单独的模块,一个使用 db 连接,另一个使用我的类

but dbSchema is a type so it cannot be embeded in my class (another type). I can create two separate modules, one with the db connection and another one with my class

module DataSource =

    [<Literal>]
    let connectionString = "Data Source=.SQLEXPRESS;Initial Catalog=Service;Integrated Security=True"

    type dbSchema = SqlDataConnection<connectionString>
    let db = dbSchema.GetDataContext()

module DealerFactory =

    type Factory(connectionString) =

        member this.GetList(latitudeLeftTop, longitudeLeftTop, latitudeRightBottom, longitudeRightBottom) =
        ".."

但是我如何在我的类的构造函数中使用 connectionString 来创建连接?

But how do I use the connectionString in my class' constructor to create the connection?

推荐答案

SQL 数据库的类型提供程序将连接字符串用于两个不同的目的.首先,它需要一个(在编译时)来生成数据库模式.其次,您可以(可选)在实际运行程序时为其提供另一个在运行时使用的方法.

The type provider for SQL database uses connection string for two different purposes. First, it needs one (at compile time) to generate the database schema. Second, you may (optionally) give it another one to use at runtime when actually running the program.

编译时连接字符串需要在SqlDataConnection<...>中指定为参数,运行时连接字符串可以传递给GetDataContext(...) 操作.

The compile-time connection string needs to be specified as parameter in SqlDataConnection<...> and the run-time connection string can be passed to GetDataContext(...) operation.

因此,您可以使用静态已知的编译时连接字符串定义您的类型:

So, you can define your type using statically known compile-time connection string:

[<Literal>]
let connectionString = "Data Source=.SQLEXPRESS;Initial Catalog=Service; ..."
type dbSchema = SqlDataConnection<connectionString>

当你想创建一个数据库连接的实例时,你可以传递另一个连接字符串:

And when you want to create an instance of the DB connection, you can pass it another connection string:

type Factory(connectionString) =
  // Create connection to the DB using (a different)
  // connection string specified at runtime
  let db = dbSchema.GetDataContext(connectionString)

  member this.GetList( latitudeLeftTop, longitudeLeftTop, 
                       latitudeRightBottom, longitudeRightBottom) =
    // Use local 'db' to access the database
    query { for v db.Table do select v }

与您的原始代码(模块中的 db 值)相比,不同之处在于这会为每个 Factory<创建一个新的 db 实例/code>,但如果 Factory 将连接字符串作为参数,我想这是预期的.

Compared with your original code (with the db value in a module), there is a difference that this creates a new db instance for every Factory, but I guess this is expected if Factory takes the connection string as an argument.

这篇关于类中 SQL 的 F# 类型提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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