"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Deploying Database Role Memberships to Test Environments

by Jason Lee

Download PDF

This topic describes how to add user accounts to database roles as part of a solution deployment to a test environment.

When you deploy a solution containing a database project to a staging or production environment, you typically don’t want the developer to automate the addition of user accounts to database roles. In most cases, the developer won’t know which user accounts need to be added to which database roles, and these requirements could change at any time. However, when you deploy a solution containing a database project to a development or test environment, the situation is usually rather different:

  • The developer typically re-deploys the solution on a regular basis, often several times a day.
  • The database is typically re-created on every deployment, which means that database users must be created and added to roles after every deployment.
  • The developer typically has full control over the target development or test environment.

In this scenario, it’s often beneficial to automatically create database users and assign database role memberships as part of the deployment process.

The key factor is that this operation needs to be conditional based on the target environment. If you’re deploying to a staging or a production environment, you want to skip the operation. If you’re deploying to a developer or test environment, you want to deploy role memberships without further intervention. This topic describes one approach you can use to address this challenge.

This topic forms part of a series of tutorials based around the enterprise deployment requirements of a fictional company named Fabrikam, Inc. This tutorial series uses a sample solution—the Contact Manager solution—to represent a web application with a realistic level of complexity, including an ASP.NET MVC 3 application, a Windows Communication Foundation (WCF) service, and a database project.

The deployment method at the heart of these tutorials is based on the split project file approach described in Understanding the Project File, in which the build process is controlled by two project files—one containing build instructions that apply to every destination environment, and one containing environment-specific build and deployment settings. At build time, the environment-specific project file is merged into the environment-agnostic project file to form a complete set of build instructions.

Task Overview

This topic assumes that:

To create database users and assign role memberships when you deploy a database project to a test environment, you’ll need to:

This topic will show you how to perform each of these procedures.

Scripting the Database Role Memberships

You can create a Transact-SQL script in a lot of different ways, and in any location you choose. The easiest approach is to create the script within your solution in Visual Studio 2010.

To create a SQL script

  1. In the Solution Explorer window, expand your database project node.
  2. Right-click the Scripts folder, point to Add, and then click New Folder.
  3. Type Test as the folder name, and then press Enter.
  4. Right-click the Test folder, point to Add, and then click Script.
  5. In the Add New Item dialog box, give your script a meaningful name (for example, AddRoleMemberships.sql), and then click Add.

  6. In the AddRoleMemberships.sql file, add Transact-SQL statements that:

    1. Create a database user for the SQL Server login that will access your database.
    2. Add the database user to any required database roles.
  7. The file should resemble this:

    [!code-sqlMain]
       1:  USE $(DatabaseName)
       2:  GO
       3:  CREATE USER [FABRIKAM\TESTWEB1$] FOR LOGIN[FABRIKAM\TESTWEB1$]
       4:  GO
       5:  USE [ContactManager]
       6:  GO
       7:  EXEC sp_addrolemember N'db_datareader', N'FABRIKAM\TESTWEB1$'
       8:  GO
       9:  USE [ContactManager]
      10:  GO
      11:  EXEC sp_addrolemember N'db_datawriter', N'FABRIKAM\TESTWEB1$'
      12:  GO
  8. Save the file.

Executing the Script on the Target Database

Ideally, you’d run any required Transact-SQL scripts as part of a post-deployment script when you deploy your database project. However, post-deployment scripts don’t allow you to execute logic conditionally based on solution configurations or build properties. The alternative is to run your SQL scripts directly from the MSBuild project file, by creating a Target element that executes a sqlcmd.exe command. You can use this command to run your script on the target database:

[!code-consoleMain]

   1:  sqlcmd.exe –S [Database server] –d [Database name] –i [SQL script]

[!NOTE] For more information on sqlcmd command-line options, see sqlcmd Utility.

Before you embed this command in an MSBuild target, you need to consider under what conditions you want the script to run:

If you’re using the split project file approach described in Understanding the Project File, as demonstrated by the Contact Manager sample solution, you can split the build instructions for your SQL script like this:

In the environment-specific project file, you need to define the database server name, the target database name, and a Boolean property that lets the user specify whether to deploy role memberships.

[!code-xmlMain]

   1:  <PropertyGroup>
   2:     <CmTargetDatabase Condition=" '$(CmTargetDatabase)'=='' ">
   3:        ContactManager
   4:     </CmTargetDatabase>
   5:     <DatabaseServer Condition=" '$(DatabaseServer)'=='' ">
   6:        TESTDB1
   7:     </DatabaseServer>
   8:     <DeployTestDBRoleMemberships Condition="'$(DeployTestDBRoleMemberships)'==''">
   9:        true
  10:     </DeployTestDBRoleMemberships>
  11:  </PropertyGroup>

In the universal project file, you need to provide the location of the sqlcmd executable and the location of the SQL script you want to run. These properties will remain the same regardless of the destination environment. You also need to create an MSBuild target to execute the sqlcmd command.

[!code-xmlMain]

   1:  <PropertyGroup>
   2:     <SqlCmdExe Condition=" '$(SqlCmdExe)'=='' ">
   3:        C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe
   4:     </SqlCmdExe>
   5:  </PropertyGroup>
   6:   
   7:  <Target Name="DeployTestDBPermissions" 
   8:          Condition=" '$(DeployTestDBRoleMemberships)'=='true' AND 
   9:                      '$(Whatif)'!='true' ">
  10:     <PropertyGroup>
  11:       <SqlScript>
  12:          $(SourceRoot)ContactManager.Database\Scripts\Test\AddRoleMemberships.sql
  13:       </SqlScript>
  14:       <_Cmd>"$(SqlCmdExe)" -S "$(DatabaseServer)" 
  15:                            -d "$(CmTargetDatabase)" 
  16:                            -i "$(SqlScript)"
  17:       </_Cmd>
  18:     </PropertyGroup>
  19:     <Exec Command="$(_Cmd)" ContinueOnError="false" />
  20:  </Target>

Notice that you add the location of the sqlcmd executable as a static property, as this could be useful to other targets. In contrast, you define the location of your SQL script and the syntax of the sqlcmd command as dynamic properties within the target, as they will not be required before the target is executed. In this case, the DeployTestDBPermissions target will only be executed if these conditions are met:

Finally, don’t forget to invoke the target. In the Publish.proj file, you can do this by adding the target to the dependency list for the default FullPublish target. You need to ensure that the DeployTestDBPermissions target is not executed until the PublishDbPackages target has been executed.

[!code-xmlMain]

   1:  <Project ToolsVersion="4.0" 
   2:           DefaultTargets="FullPublish" 
   3:           xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   4:     ...
   5:     <PropertyGroup>
   6:        <FullPublishDependsOn>
   7:           Clean;
   8:           BuildProjects;
   9:           GatherPackagesForPublishing;
  10:           PublishDbPackages;
  11:           DeployTestDBPermissions;
  12:           PublishWebPackages;
  13:        </FullPublishDependsOn>
  14:     </PropertyGroup>
  15:     <Target Name="FullPublish" DependsOnTargets="$(FullPublishDependsOn)" />
  16:  </Project>

Conclusion

This topic described one way in which you can add database users and role memberships as a post-deployment action when you deploy a database project. This is typically useful when you regularly re-create a database in a test environment, but it should usually be avoided when you deploy databases to staging or production environments. As such, you should ensure that you use the necessary conditional logic so that database users and role memberships are only created when it’s appropriate to do so.

Further Reading

For more information on using VSDBCMD to deploy database projects, see Deploying Database Projects. For guidance on customizing database deployments for different target environments, see Customizing Database Deployments for Multiple Environments. For more information on using custom MSBuild project files to control the deployment process, see Understanding the Project File and Understanding the Build Process. For more information on sqlcmd command-line options, see sqlcmd Utility.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/deploying-database-role-memberships-to-test-environments.htm
< THANKS ME>