How to: Create a Model Using Visual Studio and "M"

[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 first of five tasks that create a data-driven application by using the SQL Server Modeling CTP technologies. For an overview of this tutorial, see Getting Started with the SQL Server Modeling CTP (SetupApplication Tutorial).

In this topic, you use Microsoft code name “M” to create a model of a Windows Installer setup application. The following steps explain how to write the setup model in Visual Studio 2010. This provides a text-based view of the model that can then be loaded into a SQL Server 2008 database. See the references section at the end of this topic for links to more details on these specific technologies.

Note that this topic defines the first part of the model. It defines a module, SetupApplication, which becomes a SQL Server schema in the target database. It also defines extents that define storage, constraints, and relationships for various entities that the model contains. Note that these extents become tables in the target database. This happens after a compiled image file for the model is loaded into a SQL Server database. For more information, see the next topic in this series, How to: Load a Model into a SQL Server Database.

To create a new Visual Studio project for the SetupApplication model

  1. In Visual Studio 2010, on the File menu, select New, and then click Project.

  2. In the New Project dialog, name the project SetupApplication.

  3. In the Installed Templates list, select Visual C#.

  4. In the list of Visual C# templates, select Oslo Library.

  5. Note the location of the solution directory for future use, and then click the OK button.

To use "M" to define the SetupApplication model

  1. In Solution Explorer, rename the Model.m file to SetupApplication.m.

  2. Open the SetupApplication.m file, and delete its content.

  3. Define an “M” module named SetupApplication that scopes this new model. In the “M” language, a module is a mandatory structure that contains a set of related types; this is similar to a namespace in C#. For more information about how modules are used in “M”, see Modules ("M" Programming Guide) in the "M" Programming Guide.

    module SetupApplication
    {
    
    }
    
  4. There are several important concepts required to model a setup application. These include: products, packages, mediums, features, directories, components, and files. Within the braces of the SetupApplication module, create an extent for each entity that describes their characteristics. For more information, see  Extents and Types in the "M" Programming Guide.

        Products :
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                Name : Text;
    
                ProductId : Guid;
    
                UpgradeCode : Guid;
    
                Language : Integer32 => 1033; // English - United States
    
                Codepage : Integer32 => 1252; // Western European Latin
    
                Version : Text;
    
                Manufacturer : Text;
                    
            }
        )*} where identity Id;
    
    
        Packages : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                Product : Products;
    
                Keywords : {Text*};
    
                Description : Text?;
    
                Comments : Text?;
    
                Manufacturer : Text;
    
                InstallerVersion : Integer32 => 200; // Windows Installer 2.0
    
                Language : Integer32 => 1033; // English - United States
    
                Compressed : Logical => true;
    
            }
        )*} where identity Id;
    
        
        Media : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                Product : Products;
    
                Cabinet : Text;
    
                EmbedCab : Logical;
    
            }
        )*} where identity Id;
        
        
        Features : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                Product : Products;
    
                Level : Integer32 => 1;
    
                ComponentRefs : {Components*};
    
            }
        )*} where identity Id;
    
    
        Directories : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                SpecialType : Text?;
    
                Product : Products;
    
                ParentDirectory : Directories?;
    
                Name : Text?;
    
            }
        )*} where identity Id;
                
    
    
        Components : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                ComponentGuid : Guid => NewGuid();
    
                Directory : Directories;
    
            }
        )*} where identity Id;
    
    
        Files : 
        {(
            {
                Id : Integer64 => AutoNumber();
                    
                Component : Components;
                    
                Name : Text;
    
                Source : Text;
    
            }
        )*} where identity Id;
    
    
    Dd857490.note(en-us,VS.85).gifNote:
    Typically, you should separate each extent into its own “M” source file. In this example, multiple extents are located in the same source file to reduce complexity. For more information about similar coding conventions, see "M" Coding Conventions.

  5. Observe the connections between extents. For example, the Features extent references one product in the Products extent and zero or more components in the Components extent.

    Dd857490.note(en-us,VS.85).gifNote:
    To support references between extents, each extent must have an identity. In this example, each extent has an automatically incrementing Id field. The where clause at the end of each extent specifies that this Id field is the identity for the extent.

  6. Save the changes to the SetupApplication.m file.

  7. On the Build menu, select Build Solution.

The next step involves loading this model into a SQL Server database. For more information, see How to: Load a Model into a SQL Server Database.

Example

The following code example shows the “M” model that is built in this first task.

module SetupApplication
{
    Products :
    {(
        {
            Id : Integer64 => AutoNumber();
                
            Name : Text;

            ProductId : Guid;

            UpgradeCode : Guid;

            Language : Integer32 => 1033; // English - United States

            Codepage : Integer32 => 1252; // Western European Latin

            Version : Text;

            Manufacturer : Text;
                
        }
    )*} where identity Id;


    Packages : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            Product : Products;

            Keywords : {Text*};

            Description : Text?;

            Comments : Text?;

            Manufacturer : Text;

            InstallerVersion : Integer32 => 200; // Windows Installer 2.0

            Language : Integer32 => 1033; // English - United States

            Compressed : Logical => true;

        }
    )*} where identity Id;

    
    Media : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            Product : Products;

            Cabinet : Text;

            EmbedCab : Logical;

        }
    )*} where identity Id;
    
    
    Features : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            Product : Products;

            Level : Integer32 => 1;

            ComponentRefs : {Components*};

        }
    )*} where identity Id;


    Directories : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            SpecialType : Text?;

            Product : Products;

            ParentDirectory : Directories?;

            Name : Text?;

        }
    )*} where identity Id;
            


    Components : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            ComponentGuid : Guid => NewGuid();

            Directory : Directories;

        }
    )*} where identity Id;


    Files : 
    {(
        {
            Id : Integer64 => AutoNumber();
                
            Component : Components;
                
            Name : Text;

            Source : Text;

        }
    )*} where identity Id;

}

See Also


Show: