Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing connection string to Linq-To-Sql data provider

Is there a way to provide a connection string to Linq-To-Sql data provider in F# from App.Config file.

I have tried the following just for testing:

let mutable connString = @"Data Source=PCSQLEXPRESS;Initial Catalog=NortwindDB;Integrated Security=True"
type SqlConnection = SqlDataConnection<ConnectionString = connString>

but I get an error message "This is not a constant expression or valid custom attribute value"

Thanks

like image 642
fitims Avatar asked May 09 '12 22:05

fitims


People also ask

Which LINQ API is used to connect and work with SQL Server?

LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.

Is LINQ to SQL obsolete?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

How does LINQ to SQL work?

LINQ to SQL is a mapping between the Relational Database Schema and Objects. The relational data can be manipulated using LINQ by its mapped objects. When an object is linked to relational data, it receives attributes of the relational data.


2 Answers

The type provider itself requires a hard-coded connection string for generating the type (in your case SqlConnection) to develop against at compile time, but, you can configure the actual connection string used at runtime like so:

type SqlConnection = SqlDataConnection<"Data Source=PCSQLEXPRESS;Initial Catalog=NortwindDB;Integrated Security=True">
let runtimeConnStr = ...
type dataContext = SqlConnection.GetDataContext(runtimeConnStr)
like image 89
Stephen Swensen Avatar answered Oct 22 '22 01:10

Stephen Swensen


Maybe using the "?ConnectionStringName" parameter will get you where you want.

http://msdn.microsoft.com/en-us/library/hh362320(v=VS.110).aspx

like image 42
Jizugu Avatar answered Oct 21 '22 23:10

Jizugu