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

查看:45
本文介绍了类中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是一种类型,因此无法将其嵌入到我的类中(另一种类型).我可以创建两个单独的模块,一个与数据库连接,另一个与我的班级

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实例,但是我想如果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天全站免登陆