How to: Add a User to a Role

You can add a user and a role to a database project by using Schema View. However, you must manually associate the user with the role in the post-deployment script. For more information about post-deployment scripts, see How to: Specify Pre-Deployment or Post-Deployment Scripts.

To add a user, add a role, and associate the user with that role

  1. Open or create a database project. For more information, see How to: Create Database and Server Projects or How to: Open a Database or Server Project.

  2. If Schema View does not appear, open the View menu, and click Database Schema View.

  3. If you have already created or imported the user, go to step 7.

  4. In Schema View, right-click the Security folder, point to Add, and click User.

    The Add New Item dialog box appears.

  5. In Name, type the name of the user whom you want to create.

  6. Click Add.

    The user is created and added to the database project. The file that contains the user definition appears in Solution Explorer. The database object for the user appears in Schema View.

  7. If you have already created or imported the role, go to step 11.

  8. In Schema View, right-click the Security folder, point to Add, and click Database Role.

    The Add New Item dialog box appears.

  9. In Name, type the name of the role that you want to create.

  10. Click Add.

    The database role is created and added to the database project. The file that contains the database role definition appears in Solution Explorer. The database object for the role appears in Schema View.

  11. In Solution Explorer, expand the Scripts node, expand the Post-Deployment node, and double-click Script.PostDeployment.sql.

    The Transact-SQL (T-SQL) editor opens and displays the post-deployment script.

  12. At the bottom of the post-deployment script, add the following T-SQL statement:

    EXEC sp_addrolemember N'RoleName', N'UserName' 
    
  13. On the File menu, click Save Script.PostDeployment.sql.

  14. In Solution Explorer, right-click the database project, and click Build.

    Your database project is built, and the build script is created in the location that you specified in the database project properties. If you open the build script, the line that you added to the RoleMemberships.sql file appears in the post-deployment section of the build script.

    The next time that you deploy the database project, the user and role will be created, and the user will be associated with the role.

See Also

Concepts

Terminology Overview of Database Edition

Scenario: Create and Modify Database Scripts

Scenario: Create and Modify Database and Server Objects