How to add a database to an elastic or vCore based pool using Azure.ResourceManager.Sql package?

Christopher Clark 50 Reputation points
2024-02-06T21:29:56.35+00:00

I'm trying to write a utility to programmatically provision databases for a new environment using the Azure.ResourceManager.Sql package. I've successfully managed to add a database to a resource group, but I can't find any information in either the SqlServerData, SqlServerResource classes or the pool related classes to actually move the new database into a resource group. All the documentation I can find seems to consist of just a list of the methods without any context pertaining to specific use cases. How can I programmatically add a database to an elastic or vCore based pool, and is there a good resource out there with code examples for the latest version of the package?

Azure SQL Database
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2024-02-13T09:17:40.8+00:00

    Hi
    Christopher Clark
    •,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue:

    Solution in Customer Verbatim:

    Update, I think I finally figured it out. I was getting stuck because I was confusing a Sql Server with a Sql Database and not properly understanding the resource hierarchy.

    public async Task CreateDatabase(string name, string poolId) { var client = GetClient(); var resourceGroup = client.GetDefaultSubscription().GetResourceGroup("xxx-xxx").Value; var server = await resourceGroup.GetSqlServerAsync("xxx-xxx"); var pool = await server.Value.GetElasticPoolAsync(poolId); var sqlDatabaseData = new SqlDatabaseData(Azure.Core.AzureLocation.CentralUS); sqlDatabaseData.ElasticPoolId = new Azure.Core.ResourceIdentifier(pool.Value.Id); var result = await server.Value.GetSqlDatabases().CreateOrUpdateAsync(Azure.WaitUntil.Completed, name, sqlDatabaseData);

    If you have any other questions or are still running into more issues, please let me know.

    Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.


2 additional answers

Sort by: Most helpful
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2024-02-07T03:49:09.06+00:00

    Hi
    Christopher Clark
    •,

    Welcome to Microsoft Q&A forum.

    As I understand, you want to add a database to an elastic or vCore based pool programmatically. Please check this Stackoverflow link if this helps for your case:

    https://stackoverflow.com/questions/31436924/how-to-add-a-sql-database-to-an-elastic-pool-programmatically

    Let us know if you have further queries. Please share existing setup and sample code so that we can refer and help.

    Thanks

    0 comments No comments

  2. Christopher Clark 50 Reputation points
    2024-02-12T15:55:11.67+00:00

    Update, I think I finally figured it out. I was getting stuck because I was confusing a Sql Server with a Sql Database and not properly understanding the resource hierarchy.

    public async Task CreateDatabase(string name, string poolId)
    {
    			var client = GetClient();
                var resourceGroup = client.GetDefaultSubscription().GetResourceGroup("xxx-xxx").Value;
                var server = await resourceGroup.GetSqlServerAsync("xxx-xxx");
                var pool = await server.Value.GetElasticPoolAsync(poolId);
                var sqlDatabaseData = new SqlDatabaseData(Azure.Core.AzureLocation.CentralUS);
                sqlDatabaseData.ElasticPoolId = new Azure.Core.ResourceIdentifier(pool.Value.Id);
                var result = await server.Value.GetSqlDatabases().CreateOrUpdateAsync(Azure.WaitUntil.Completed, name, sqlDatabaseData);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.