Install the addon

Go to the ElephantSQL addon page, choose plan and the application to which you would like to add ElephantSQL.

Restart your application and the URL to the database will be available from ConfigurationManager.AppSettings["ELEPHANTSQL_URL"].

Connect to the database

The best PostgreSQL client library for .NET is Npgsql, it's full featured and well maintained.

To parse the connection string you can use the following class:

public class ConnStr
{
    public static string Get()
    {
        var uriString = ConfigurationManager.AppSettings["ELEPHANTSQL_URL"] ?? 'postgres://localhost/mydb;
        var uri = new Uri(uriString);
        var db = uri.AbsolutePath.Trim('/');
        var user = uri.UserInfo.Split(':')[0];
        var passwd = uri.UserInfo.Split(':')[1];
        var port = uri.Port > 0 ? uri.Port : 5432;
        var connStr = string.Format("Server={0};Database={1};User Id={2};Password={3};Port={4}", 
            uri.Host, db, user, passwd, port);
        return connStr;
    }
}

Example

At github.com/ElephantSQL/PgSimpleDataTodoExample there's a example application which uses Simple.Data as the ORM.

Migrate between plans

Currently you can only upgrade/downgrade between the shared plans. If you want to upgrade to a dedicated server we suggest that you use pg_dump to backup the database, delete the addon, add the new plan and then import the old database.

Entity Framework - npgsql workaround

Entity Framework is an Object Relational Mapper (ORM) for ADO.NET, a part of the .NET Framework. A common error when trying to connect to the database is:

[NpgsqlException: FATAL: 28000: no pg_hba.conf entry for host "host", user "username", database "postgres", SSL off]

There's a work around to solve this problem. Entity Framework allows you to inject a DbConfiguration which can include a custom ManifestTokenResolver. Below is code for reference based on http://stackoverflow.com/a/19506933. It requires hard-coding the server version, however, this is okay because as far as we can tell Npgsql.EntityFramework is only using the server version to enforce it is over "8.1.0". This also has the bonus of cutting down on connections to the database since it no longer needs a round trip to fetch the server version each time a DbContext is instantiated.

[DbConfigurationType(typeof(ElephantSqlDbConfiguration))]
public class AuctionContext : DbContext
{
    //Create DBSets
    public AuctionContext()
        : base(new Npgsql.NpgsqlConnection(Core.Persistence.Connections.GetDbString()), true)
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Build the model
        base.OnModelCreating(modelBuilder);
    }
}

internal sealed class ElephantSqlDbConfiguration : DbConfiguration
{
    private const string ManifestToken = @"9.2.8";
    public ElephantSqlDbConfiguration()
    {
        this.AddDependencyResolver(new SingletonDependencyResolver(new ManifestTokenService()));
    }
    private sealed class ManifestTokenService : IManifestTokenResolver
    {
        private static readonly IManifestTokenResolver DefaultManifestTokenResolver 
          = new DefaultManifestTokenResolver();
        public string ResolveManifestToken(DbConnection connection)
        {
            if (connection is NpgsqlConnection)
            {
                return ManifestToken;
            }
            return DefaultManifestTokenResolver.ResolveManifestToken(connection);
        }
    }
}

Help

If you need further support we are happy to help you, just email us at support@elephantsql.com.