Search code examples
azure-sql-databaseazure-sdk-.netazure-sdk

Create a Sql Azure Database with serverless tier using SDK


Currently, I create databases and attach them to an SQL elastic pool:

database = await sqlServer.Databases.Define(mainDb.DbName).WithExistingElasticPool(pool.Name).CreateAsync();

Instead, I want to create databases with tier "General Purpose: Serverless, Gen5, 1 vCore", but I couldn't find any method that offers that possibility.

This feature is still in preview, I can't find anything on the forums on this. How can I achieve this?


Solution

  • According to my test, we can use the following c# code to create "General Purpose: Serverless, Gen5, 1 vCore" database

    var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(client,key,tenant,AzureEnvironment.AzureGlobalCloud);
    var azure = Azure.Configure().Authenticate(credentials).WithSubscription(SubscriptionId);
    var sqlserver=azure.SqlServers.GetById("/subscriptions/<your subscrption id>/resourceGroups/<your resource group name>/providers/Microsoft.Sql/servers/<your server name>");
    var database = sqlserver.Databases.Define("test").WithEdition("GeneralPurpose").WithServiceObjective("GP_S_Gen5_1").Create();
    Console.WriteLine(database.ServiceLevelObjective);
    Console.WriteLine(database.Edition);
    Console.WriteLine(database.Name);
    Console.ReadLine();
    

    enter image description here