Compartir a través de


How to: Create data using "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.]

In addition to creating models, Microsoft code name “M” is able to add model instance data to an existing model in the SQL Server Modeling Services database. This topic demonstrates how to insert rows into the database by using “M” and the SQL Server Modeling CTP tools.

Note

This topic uses a HumanResources model. For more information about how to create this model and install it in the Modeling Services database, see How to: Install the HumanResources Sample Model using Visual Studio.

In this tutorial, you will perform the following tasks:

  • Use Mx.exe to create a new Folder for containing Employees.

  • Create an “M” file to define new employee instances.

  • Compile and load the model instances into the Modeling Services database.

  • Verify the new employee instances.

  • Use Mx.exe to remove the Folder for this example.

This topic shows how to add this data by using the SQL Server Modeling CTP tools. It is also possible to add instances by using Visual Studio 2010. For an example that adds models instance data by using Visual Studio 2010, see Getting Started with the SQL Server Modeling CTP (SetupApplication Tutorial).

To create a new Modeling Services Folder

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

  2. Use the following command to create a new Modeling Services Folder named HumanResourcesFolder. The system-provided Repository Folder is the parent Folder.

    mx createFolder Repository/HumanResourcesFolder /database:Repository
    

To create "M" Employee instances

  1. At the same command line, navigate to the bin\Debug directory of the HumanResources project. For more information about how to create this project, see How to: Install the HumanResources Sample Model using Visual Studio.

    Tip

    If you don't remember the location of your project directory, go to Visual Studio 2010 and open the HumanResources project. Right-click the project in Solution Explorer, and then click Open Folder in Windows Explorer. Use Windows Explorer to discover the location of the project.

  2. At the command prompt, type the following command and press ENTER.

    notepad.exe Employee_Instances.m
    
  3. At the dialog, click Yes to create the new file.

  4. In Notepad, add the following “M” instance definitions. This inserts two employees, John and Susan. The Id field is not provided, because it is automatically assigned. The PathsFolder function returns an identifier for the Folder specified by the path, Repository/HumanResourcesFolder. This path matches the Folder created in the previous step. The identifier is then passed to the FoldersTable to return the Folder entity that matches that identifier. The PathsFolder function is defined in the Repository.Item model, so the code imports the Repository.Item module.

    module HumanResources
    {
        import Repository.Item;
    
        Employees
        {
            FirstEmployee
            {
                Folder => FoldersTable(PathsFolder("Repository/HumanResourcesFolder")),
                Name => "John",
            },
            SecondEmployee
            {
                Folder => FoldersTable(PathsFolder("Repository/HumanResourcesFolder")),
                Name => "Susan",
            }
        }
    }
    
  5. On the File menu, click Save.

  6. Close Notepad.

To load the Employee instances into the Modeling Services database

  1. At the original command prompt, use the “M” compiler to create an image file for the instance data. Note that you must reference both the HumanResources.dll assembly and the system-provided Repository.dll assembly to resolve the dependencies in the Employee_Instances.m source code.

    m.exe Employee_Instances.m /package:Image /out:Employee_Instances.mx /r:HumanResources.dll /r:"C:\Program Files\Microsoft Oslo\1.0\bin\Repository.dll"
    

    Note

    The Repository.dll file is in the bin directory of the SQL Server Modeling CTP. If you installed the SQL Server Modeling CTP to a different path than the one in the previous command, you must modify the command to the correct location for Repository.dll.

  2. Use the dir command to verify the creation of the Employees_Instances.mx image file.

  3. Install the image into the local Modeling Services database.

    mx.exe install Employee_Instances.mx /database:Repository
    

    Note

    You can safely ignore the M6046 warning about the unexpected database collation.

To verify the new Employee instances

  1. At the command prompt, type sqlcmd.exe and press ENTER.

    sqlcmd.exe
    
  2. Type the following lines, pressing ENTER between each line.

    use Repository
    go
    select * from [HumanResources].[Employees]
    go
    
  3. Verify that the new data has been added to the model. There should be two employees returned from the query.

  4. Type quit to exit.

To remove the HumanResourcesExampleFolder Folder

  1. At the command prompt, type the following command and press ENTER.

    mx.exe uninstall Employee_Instances /d:Repository
    
  2. Type the following command and press ENTER. This removes the Folder as well as the rows in the [HumanResources].[Employees] table that refer to that Folder.

    mx.exe dropFolder Repository/HumanResourcesFolder /database:Repository
    

See Also

Other Resources

Loading Domain Models with Data
Using "M" Tools