Engineering

ORMs on the Apprenda Platform

By Bryan Gregorius4.10.13

One question we often receive is if the Apprenda Platform supports the use of ORMs within guest applications. We not only support ORMs but we make it easy for you as developers to configure connections to your databases regardless of where on the platform they end up being deployed. This issue really boils down to one thing: Where do I get my Connection String from?

Connection Strings

All ORMs require you to configure or initialize them with the location of your database; this is typically done by specifying the connection information via a Connection String. By allowing the Apprenda platform to manage your database you give up control over where your database is going to be located. The Apprenda platform provides a few different approaches to determining what the Connection String is for your application.

One way to obtain the proper Connection String is to leverage the $#APPLICATIONCONNECTIONSTRING#$ configuration token in your App.config or Web.config which will be replaced during deployment with the actual value. The main drawback to this approach is that it is not dynamic so if you relocate your database your currently deployed workloads will no longer be able to contact your database. Another limitation to this approach is that you cannot leverage the Apprenda platform’s ability to add multi-tenancy to your applications. For these reasons we recommend only using this approach in single-tenant applications on platforms that only have a single database instance exposed to the platform. The main benefit to this approach is it typically requires no actual code changes to an existing application if the application was already retrieving its Connection String from configuration.

The more flexible way of obtaining the correct Connection String is through the API provided TenantContext object. TenantContext.Current.ConnectionString will provide you with a Tenant specific connection string to your database. This approach will support both single and multi-tenant deployments as well as dynamically retrieve the proper Connection String at runtime if you relocate your database. While this is the most flexible approach it does require some code modifications and additional references for your application.

Some Examples

To illustrate the different approaches I will show examples of how to configure both Entity Framework and NHibernate to run on the platform.

Configuration Based Approach

You can see here the use of the platform’s ability to perform local / live configuration switching. This allows the developer to develop locally using a locally configured SQL instance while using the Application Connection String token when deployed on the platform.

The Entity Framework implementation in this example is using configuration by convention. The Connection String is being matched up to the defined DbContext by name, in this case TimeCardContext:

The NHibernate example is a little more verbose but results in the same thing: 

In both cases the Connection String is being read out of configuration. When not deployed on the platform it will use what is configured locally and when deployed to the platform the local / live switching and token replacement will occur.

Runtime Configuration Approach

If we wanted to change the application to support Multi-tenancy we need to make use of the Apprenda API provided in the SDK. The Entity Framework example above could be modified to support multi-tenancy by simply creating a constructor for TimeCardContext that pulled the Connection String at runtime out of TenantContext.

In much the same way the NHibernate example above can be easily modified to pass in the raw Connection String at runtime.

The Apprenda platform makes using ORMs as part of your application easy and convenient, even in situations where you don’t know where your database will be deployed or if your application will be multi-tenant.

For more information about developing applications for the Apprenda platform visit our Developer Documentation here.

Bryan Gregorius

Bryan Gregorius is a Senior Client Solutions Architect on Apprenda’s client services team. He works daily with analysts and developers to gather requirements, design, and develop highly technical solutions that leverage the Apprenda platform for customers.

2
View Comments
  1. ruthvikOctober 25, 2016

    hi,
    I’m working on a classic asp application it is working fine on iis. I’m trying to transform it to apprenda along with application provisioning script.this is my connection string for local iis
    sConnessione = “Provider = SQLOLEDB;Driver=SQLOLEDB;Data Source=Machinename;Initial Catalog=Myapp;Persist Security Info=True;User ID=ruthvik;Password=@Ruthvik”
    Apprenda generates a connection string in place $#APPLICATIONCONNECTIONSTRING#$

    Data Source=WIN-02\APPRENDA;Initial Catalog=Myapp–v1;Integrated Security=False;User ID=tenant-746431e8-aad9-4220-8e0e-8436163f4631;Password=5e7b8b7b-40d9-4367-b2a5-0760eeb994e9;Pooling=True;MultipleActiveResultSets=False”

    it is creating database with tables, but don’t know what is the issue it is giving the following error
    Microsoft OLE DB Provider for ODBC Drivers error ‘80004005’

    [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

  2. Kyle DeweerdtFebruary 1, 2017

    Hi, thank you for your question. I’ve spoken to our support team and they would be happy to help. Please send them an email, support(at)apprenda(dot)com.

    Kind regards,
    Kyle

Leave a Reply

Your email address will not be published. Required fields are marked *