4

I use EF 6. My existing code is :

public void CreateOrUpdateCompanyDb(string companyDbName)
{
        try
        {
            string connectionString = _connectionStringProvider.GetConnectionString(companyDbName);
            DbMigrationsConfiguration cfg = CreateMigrationsConfig(connectionString);
            cfg.AutomaticMigrationsEnabled = false;
            cfg.AutomaticMigrationDataLossAllowed = false;
            DbMigrator dbMigrator = new DbMigrator(cfg);

            dbMigrator.Update();
        }
        catch (MigrationsException exception)
        {
            _logger.Error(string.Format("Error creating company database '{0}'",companyDbName), exception);
        }
}

with connection string as follows :

Server=tcp:xxx.database.windows.net,1433;Database=companyDbName;User ID=xxx@xxx;Password=xxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"

which creates the database for the particular company. But the problem is that the created database is from the now retired Web Edition but I want to create Basic/Standard/Premium edition.

How should I manipulate the connection string so that the edition of the database is the desired one?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70

1 Answers1

8

The Edition of Sql Azure Database is something you can specify in Create Database command. AFAIK, you can't specify it using the connection string.

Syntax -

CREATE DATABASE database_name [ COLLATE collation_name ]
{
   (<edition_options> [, ...n]) 
}

<edition_options> ::= 
{
      MAXSIZE = { 100 MB | 500 MB | 1 | 5 | 10 | 20 | 30 … 150…500 } GB  
    | EDITION = { 'web' | 'business' | 'basic' | 'standard' | 'premium' } 
    | SERVICE_OBJECTIVE = { 'shared' | 'basic' | 'S0' | 'S1' | 'S2' | 'P1' | 'P2' | 'P3' } 
}
[;]

Given that, for your scenario, two options come to mind -

  1. Before using DbMigrator, explicitly write code which creates the database, if it does not exist using traditional ADO.Net.

  2. The other option which comes to mind, but I don't know enough about and you could dig into if you want to, is to somehow find a way to hook into EF, so that you could customize the Create Database command it must generate.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
  • I will test it and if it works fine I will mark your answer. Thank you for your time to answer the question. – Ognyan Dimitrov Jan 02 '15 at 11:29
  • 3
    For all who stumble upon : another approach [here](https://social.technet.microsoft.com/Forums/office/en-US/7d7b5cd8-5878-4241-a674-33336010f081/set-service-tiers-when-create-azure-sql-database-from-vs-c-entity-framework?forum=ssdsgetstarted) and another one [here](http://blog.siliconvalve.com/2014/11/12/use-azure-management-api-sdk-in-an-entity-framework-custom-database-initializer/) – Ognyan Dimitrov Mar 09 '15 at 13:54
  • 1
    Further to @OgnyanDimitrov's comment above, yep, I found the IDatabaseInitializer approach from https://social.technet.microsoft.com/Forums/office/en-US/7d7b5cd8-5878-4241-a674-33336010f081/set-service-tiers-when-create-azure-sql-database-from-vs-c-entity-framework to work best for me. – Tim Iles Nov 24 '15 at 16:37