How to: Describe Required Patterns with the PatternApplication Sample

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

This is the second of six tasks that apply SQL Server Modeling Services patterns to a Microsoft code name “M” model. The PatternApplication sample provides a declarative technique for describing which Modeling Services patterns to apply to a module or extent in Microsoft code name “M”. For more information about installing and using the PatternApplication sample, see Using the PatternApplication Sample.

This topic shows how to use the PatternApplication sample to describe which Modeling Services patterns to apply to the SetupApplication model. In the previous step of this tutorial, you made changes to the SetupApplication model to support Modeling Services patterns. For more information, see  How to: Modify a Domain to Support SQL Server Modeling Services Patterns.

Before using the PatternApplication sample, you must first install the Base Domain Library (BDL) in the SetupApplicationDB database. For more information about how to create the SetupApplicationDB database, see How to: Load a Model into a SQL Server Database. The BDL provides the infrastructure that supports Modeling Services patterns. The PatternApplication sample can be installed into any database that contains the BDL. The first step of this topic describes how to install both the BDL and the PatternApplication sample.

Note

It is necessary to install the BDL into the SetupApplicationDB because this is a custom Modeling Services database and the BDL is not installed automatically. If you use the system-provided Repository database, the BDL is already installed.

To install the Base Domain Library and the PatternApplication sample

  1. On the Start Menu, click All Programs, Microsoft SQL Server Modeling CTP, and open the Microsoft SQL Server Modeling CTP Command Prompt.

  2. Install the Repository.mx image file that contains the Base Domain Library. For more information about the BDL and the installation options, see Base Domain Library (BDL).

    mx.exe install Repository.mx /database:SetupApplicationDB /server:(local) /property:rct=+ /property:ra=+
    
  3. Install the PatternApplication sample by following the steps in the topic How to: Install the PatternApplication Sample

    Note

    It is important to verify that you install the PatternApplication into the SetupApplicationDB database. This means specifying SetupApplicationDB after the database option of the Mx.exe tool as shown in the following command: mx.exe install PatternApplication.mx /database:SetupApplicationDB.

To add a reference to the PatternApplication.dll assembly

  1. In Visual Studio 2010, open the SetupApplication project.

  2. To use the PatternApplication sample, you must add a reference to the PatternApplication assembly. In Solution Explorer, right-click References, and then click Add Reference.

  3. In the Add Reference dialog, select the Browse tab.

  4. Navigate to the location of the PatternApplication.dll assembly. Select this assembly, and then click OK.

To add a PatternApplicationInstances.m file to the SetupApplication project

  1. In Solution Explorer, right-click the SetupApplication project, select Add, and then click New Item.

  2. In the Add New Item dialog, select the "M" Model Data template. Name the new file PatternApplicationInstances.m, and then click Add.

  3. In the Entity Data Model Wizard dialog, select Default Model, and then click Finish.

  4. In the code editor, delete the contents of the PatternApplicationInstances.m file.

  5. Scope the data instances with the module name of the PatternApplication model.

    module PatternApplication
    {
    
    }
    
  6. In the PatternApplication module, import the SetupApplication module. This is necessary to describe the patterns to apply to specific extents within the SetupApplication module.

        import SetupApplication;
    
  7. After the import statement, add one instance of the ModulePatternApplications extent for the SetupApplication module. This alters the permissions on the SetupApplication database schema associated that is with this module.

        ModulePatternApplications
        {
            SetupApplication_Pattern
            {
                Module => about(SetupApplication.ProductsTable).Declaration.DefinedIn,
                Pattern => { Patterns.AlterSchemaPermissions }
            }
        }
    
  8. Next add one instance of the EntityPatternApplications extent for each extent that requires Modeling Services patterns. In this example, the views that are associated with each extent become updatable, a foreign key is created for the Folder column to the [Repository.Item].[FoldersTable] table, auditing is enabled for the table, and change tracking is enabled for the table.

        EntityPatternApplications  
        {
            ProductsTable_Pattern
            {
                Module => about(SetupApplication.ProductsTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.ProductsTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },       
    
            PackagesTable_Pattern
            {
                Module => about(SetupApplication.PackagesTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.PackagesTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },       
    
            MediaTable_Pattern
            {
                Module => about(SetupApplication.MediaTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.MediaTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },  
    
            FeaturesTable_Pattern
            {
                Module => about(SetupApplication.FeaturesTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.FeaturesTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },  
    
            DirectoriesTable_Pattern
            {
                Module => about(SetupApplication.DirectoriesTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.DirectoriesTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },  
    
            ComponentsTable_Pattern
            {
                Module => about(SetupApplication.ComponentsTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.ComponentsTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            },  
    
            FilesTable_Pattern
            {
                Module => about(SetupApplication.FilesTable).Declaration.DefinedIn,
                Extent => about(SetupApplication.FilesTable).Declaration,
                Pattern =>
                {
                    Patterns.AddViewsInsteadOfTriggers,
                    Patterns.AddFolderForeignKey,
                    Patterns.AddAuditing,
                    Patterns.AddChangeTracking
                }
            }                                                 
        }
    

    Note

    For more information about the patterns used in this example, see Using the PatternApplication Sample.

  9. On the Build menu, click Build Solution.

  10. Verify that the build succeeded.

    Note

    You can ignore any warnings about EDM generation.

    Warning

    Do not yet deploy this model to the SetupApplicationDB database. There are complications for repeated deployments that are discussed later in this tutorial.

Although we have described the Modeling Services patterns by using the PatternApplication sample, we must call a stored procedure to actually apply them to the model in the database. The next step shows the recommended way to accomplish this by using a Post.sql script file. For more information, see How to: Add a Post.sql File to the SetupApplication Project

Example

The following example shows the complete PatternApplicationInstances.m file.

module PatternApplication
{
    import SetupApplication;

    ModulePatternApplications
    {
        SetupApplication_Pattern
        {
            Module => about(SetupApplication.ProductsTable).Declaration.DefinedIn,
            Pattern => { Patterns.AlterSchemaPermissions }
        }
    }
    
    EntityPatternApplications  
    {
        ProductsTable_Pattern
        {
            Module => about(SetupApplication.ProductsTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.ProductsTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },       
        
        PackagesTable_Pattern
        {
            Module => about(SetupApplication.PackagesTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.PackagesTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },       

        MediaTable_Pattern
        {
            Module => about(SetupApplication.MediaTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.MediaTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },  
        
        FeaturesTable_Pattern
        {
            Module => about(SetupApplication.FeaturesTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.FeaturesTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },  
        
        DirectoriesTable_Pattern
        {
            Module => about(SetupApplication.DirectoriesTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.DirectoriesTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },  
        
        ComponentsTable_Pattern
        {
            Module => about(SetupApplication.ComponentsTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.ComponentsTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        },  
        
        FilesTable_Pattern
        {
            Module => about(SetupApplication.FilesTable).Declaration.DefinedIn,
            Extent => about(SetupApplication.FilesTable).Declaration,
            Pattern =>
            {
                Patterns.AddViewsInsteadOfTriggers,
                Patterns.AddFolderForeignKey,
                Patterns.AddAuditing,
                Patterns.AddChangeTracking
            }
        }                                                 
    } 
 }

See Also

Concepts

Adding Modeling Services Patterns to the SetupApplication Model

Other Resources

Getting Started with the SQL Server Modeling CTP (SetupApplication Tutorial)