Fairly recently we had a big thread of questions regarding connection strings on our forums, so I thought I’d do a recap’ of how connection strings are handled in CodeFluent Entities.
First it’s important to separate two distinct stages:
- generation time: the connection string used by your producer to generate code,
- run time: the connection string used by your generated application.
At Generation Time
As mentioned in previous posts (see Environment Variables Support and Default Persistence Server), CodeFluent Entities uses a default connection string which is as follows:
Application Name=[DefaultNamespace];server=[DefaultPersistenceServer];database=[DefaultNamespace];Integrated Security=true;
- [DefaultNamespace]: your CodeFluent Entities default namespace,
- [DefaultPersistenceServer]: value of the CF_DEFAULT_PERSISTENCE_SERVER environment variable or “127.0.0.1” if not found.
As a consequence, by setting the CF_DEFAULT_PERSISTENCE_SERVER environment variable (see Default Persistence Server for more info), you won’t even have to specify connection strings to build your applications on your development environment.
What’s even better, is that if you’re working as a team on the same application, if each member specified its own persistence server, they won’t even have to check-out and modify the model to build it in their respective environment.
This being said, in some situations, you might have to set a specific connection string to build somewhere else than on your default persistence server. To do so, you’ve got several options:
- On your project, set the “Default Connection String” property to the default connection string to use (if none specified, the default one is used)
- Or you can specify it producer by producer (if no connection string set, the one defined at the project level is used)
At Run Time
Now that you generated your application, it can run anywhere, and connect to any persistence server.
Once again, just like at generation time, the same connection string is used by default:
Application Name=[DefaultNamespace];server=[DefaultPersistenceServer];database=[DefaultNamespace];Integrated Security=true;
So likewise, if you specified the CF_DEFAULT_PERSISTENCE_SERVER environment variable on your machine, you won’t have to define a connection string.
If your application runs on a machine without this variable, or if you want it to use another persistence server explicitly, you’ll need to set its connection string in its application configuration file (app.config for a desktop app, or web.config for a web app). Here’s the corresponding documentation article illustrating how to do it: Application Configuration.
Another point of interest is that you can reuse connection strings defined in the standard .NET <connectionStrings> element.
Example:
<configuration> <configSections> <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/> </configSections> <connectionStrings> <add name="SqlServer" connectionString="server=MYSERVER;database=Sample;Integrated Security=true" /> <add name="SqlServerExpress" connectionString="server=MYSERVER\SQLEXPRESS;database=Sample;Integrated Security=true" /> </connectionStrings> <Sample connectionString="{SqlServerExpress}" /> </configuration>
The example above we’ll use the connection string named “SqlServerExpress”.
Another point of interest is that you can also use environment variables as the connection string name, so that based on an environment variable you can go pick one connection string or the other. For instance, depending on the local machine name, you could connect to one persistence server or another:
<configuration> <configSections> <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/> </configSections> <connectionStrings> <add name="Foo" connectionString="server=MyServer1;database=Sample;Integrated Security=true" /> <add name="Bar" connectionString="server=MyServer2;database=Sample;Integrated Security=true" /> </connectionStrings> <Sample connectionString="{%COMPUTERNAME%}" /> </configuration>
In the example above, if we’re on the “Foo” machine we’ll connect to “MyServer1” whereas when we’re on the “Bar” machine, we’ll connect to the server “MyServer2”.
Hope this helps,
Carl Anderson
