CodeFluent Entities Documentation
Application Configuration
Send comments on this topic.
CodeFluent Entities > Developer Guide > The Business Object Model > Application Configuration

Glossary Item Box

The Business Object Model (BOM) is the only layer which has a direct access to the data layer; hence you have to configure its connection settings to it. To do so, CodeFluent provides in the CodeFluent.Runtime.dll a configuration section handler. Consequently, all you have to do to configure your application's connection settings, is to create a configuration section of the BOM's namespace, and in this section, specify the connection string.

Configuration Sample Copy Code
<configuration>
  <configSections>
    <section name="MyDefaultNamespace" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
  </configSections>
  <MyDefaultNamespace connectionString="database=[MyDatabaseName];server=[ServerName];Trusted_Connection=true" />
</configuration>

As you can see in the sample above:

  1. Declare a new configuration section with a section name matching your default namespace,
  2. Add a section corresponding to the declared name (your default namespace),
  3. Set the connection string which the application will be using at runtime when interacting with the database layer.

 

By default CodeFluent Entities uses this connection string:

Default Connection String Copy Code
Application Name=[DefaultNamespace];server=127.0.0.1;database=[DefaultNamespace];Integrated Security=true
Note: Each CodeFluent Entities model contains a project element, thus the project element has an attribute named defaultNamespace which must be set. By default, this is the value used as an application name and database name in the default connection string.
Note: CodeFluent uses the environment variable named CF_DEFAULT_PERSISTENCE_SERVER as the default server. If ever this environment variable isn't set, the default value 127.0.0.1 is used. If ever an explicit connection string is specified, this value isn't used.

You can retrieve the current connection string at runtime using the following code:

View the currently used connection string at runtime Copy Code
CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get([DefaultNamespace].Constants.[DefaultNamespace]StoreName).Persistence;
System.Console.WriteLine(persistence.ConnectionString);

Sharing connection strings

As explained in the previous paragraph, the BOM needs connection string to know where to retrieve its data. If using a connection string other than the default one, it must be configured in its specific configuration section. However, if ever you're using other .NET components that also need the connection string, having it declared in a custom section implies that you will have to duplicate it.

To avoid such problems, the CodeFluent runtime also supports declaring it in the standard connectionStrings section and referring to it using the connection string name:

Multiple Connection Strings Copy Code
<configuration>
  <configSections>
    <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/>
  </configSections>
  <connectionStrings>
    <add name="SqlServer" connectionString="Application Name=Sample;server=MYSERVER;database=Sample;Integrated Security=true" />
    <add name="SqlServerExpress" connectionString="Application Name=Sample;server=MYSERVER\SQLEXPRESS;database=Sample;Integrated Security=true" />
  </connectionStrings>
  <Sample connectionString="{SqlServer}" />
</configuration>

As you can see in the example above, several connection strings can be configured in the standard connectionStrings section, and then using the {ConnectionStringName} format, the runtime will use the one corresponding to the specified name.

More than making the connection string available to other .NET components, it allows you to keep all your connection strings (development, test, production, etc.) in the configuration file. Consequently, this will ease the configuration of your application on several environments.

Combining ConnectionString Aliases with Environment Variables

Combined with the support of environment variables, this feature can be used to select a connection string automatically. For instance, COMPUTERNAME is an environment variable defined by default on machines. Using this variable as an alias to a connection string, we ensure the appropriate connection string will automatically be used depending on the computer from which the application is ran.

Automatically switch connectionString by computer name Copy Code
<configuration>
  <configSections>
    <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/>
  </configSections>
  <connectionStrings>
    <add name="DevMachine1" connectionString="Application Name=Sample;server=DevMachine1;database=Sample;Integrated Security=true" />
    <add name="DevMachine2" connectionString="Application Name=Sample;server=DevMachine2\SQLEXPRESS;database=Sample;Integrated Security=true" />
  </connectionStrings>
  <Sample connectionString="{%COMPUTERNAME%}" />
</configuration>

In the example above, the application can be ran on two computers: DevMachine1 and DevMachine2. Using this technique, without modifying the configuration, the connection string named DevMachine2 will automatically be used when the application is executed on DevMachine2 and vice versa.

 

Configuring your application for Oracle Database

Persistence Type Name

The generated Business Object Model (BOM) is independent from the persistence layer used: the code is the same whether a SQL Server or an Oracle Database is used underneath. This is possible thanks to the CodeFluent runtime which uses the application configuration to know where to connect to.

By default, the runtime considers you're using the BOM with SQL Server. Therefore if you're using an Oracle Database, you'll have to specify it in the configuration. This can be done using the persistenceTypeName attribute. Setting it to Oracle will indicate the runtime that the BOM is mapped on an Oracle Database.

Using an Oracle Database Copy Code
<configuration>
  <configSections>
    <section name="MyProject" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
  </configSections>
<MyProject connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyServer)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyService)));user id=TEST;password=MyPassword;" persistenceTypeName="Oracle" />
</configuration>
Note: If ever you have errors regarding distributed/automated transactions, OraMTS, or the "CF1082: Automatic transaction enlistment has failed" error; set Enlist=false in the connection string to disable transaction enlistment.

Oracle Default Schema

The user and schema concepts are closely related in Oracle Database. Say you used a MyProject account to generate your database, all generated components will be contained in a MyProject schema. If ever, you're using another account at runtime, say Test for instance, by default you'll be under the Test schema and data objects (packages, tables, procedures, etc.) won't be found. To solve such problem, you can either use the same account at runtime as the one used at production time, or specify in the application configuration the schema to use by default. This can be done using the oracle-defaultSchema attribute.

Using Another User at Runtime Copy Code
<configuration>
  <configSections>
    <section name="MyProject" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
  </configSections>
<MyProject persistenceTypeName="Oracle" oracle-defaultSchema="MyProject"
connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyServer)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyService)));user id=TEST;password=MyPassword;"  />
</configuration>