Skip to content

Lesson 2: Importing Data

Donald Roy Airey edited this page Feb 6, 2019 · 28 revisions

Importing Data

In this lesson we're going to cover the basics of a very powerful feature of the RestApiGenerator: resolving external identifiers. Inside the database, you want to chose an primary key based on performance or cost, you don't want to chose it based on how readable it is. This is at odds with importing data from files or external systems that likely have no idea what you're using in your database for a primary key. The solution to this problem is external identifiers as unique keys. Each table that loads data from an outside source has a key that is native to that source, or a human-readable key for scripted files. Our generated API will make the translation between the outside key and the internal key for you!

Step 1

If you haven't already installed the Data Model Generator extension, go to Lesson 1 and follow steps 1 and 2.

Step 2

Create a new project. File > New > Project

In the New Project form, select Installed > Visual C# > .NET Core. We're going to create a new ASP.NET Core Web Application project called WebApplication2.

Use the API template to create the service. Take all the default settings.

Step 3

At this point you should have a solution that looks like this:

Select the ValuesController.cs that was added by the template and delete it.

Step 4

Add the Microsoft.EntityFrameworkCore and GammaFour.Data packages using the Tools > NuGet Package Manager > Package Manager Console.

Step 5

Right-Click on the WebApplication2 project in the Solution Explorer pane and Add > New Item to the project.

Select ASP.NET Core > Data > XML File and change the name to DataModel.xsd.

Step 6

Copy and paste this source into the DataModel.xsd file that you just created.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DataModel" targetNamespace="http://tempuri.org/DataModel.xsd" xmlns="http://tempuri.org/DataModel.xsd" xmlns:mstns="http://tempuri.org/DataModel.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gfdata="urn:schemas-gamma-four-com:xml-gfdata">
  <xs:element name="DataModel">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Customer" gfdata:verbs="Delete,Get,Post,Put">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string"/>
              <xs:element name="CustomerId" gfdata:AutoIncrement="true" type="xs:int" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Order" gfdata:verbs="Delete,Get,Post,Put">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CustomerId" type="xs:int" />
              <xs:element name="OrderId" gfdata:AutoIncrement="true" type="xs:int" />
              <xs:element name="ProductId" type="xs:int" />
              <xs:element name="Quantity" type="xs:decimal" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Product" gfdata:verbs="Delete,Get,Post,Put">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ProductId" gfdata:AutoIncrement="true" type="xs:int" />
              <xs:element name="Name" type="xs:string"/>
              <xs:element name="Price" type="xs:double" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="CustomerKey" gfdata:IsPrimaryKey="true">
      <xs:selector xpath=".//mstns:Customer" />
      <xs:field xpath="mstns:CustomerId" />
    </xs:unique>
    <xs:unique name="OrderKey" gfdata:IsPrimaryKey="true">
      <xs:selector xpath=".//mstns:Order" />
      <xs:field xpath="mstns:OrderId" />
    </xs:unique>
    <xs:unique name="ProductKey" gfdata:IsPrimaryKey="true">
      <xs:selector xpath=".//mstns:Product" />
      <xs:field xpath="mstns:ProductId" />
    </xs:unique>
    <xs:keyref name="CustomerOrderdKey" refer="CustomerKey">
      <xs:selector xpath=".//mstns:Order" />
      <xs:field xpath="mstns:CustomerId" />
    </xs:keyref>
    <xs:keyref name="ProductOrderKey" refer="ProductKey">
      <xs:selector xpath=".//mstns:Order" />
      <xs:field xpath="mstns:ProductId" />
    </xs:keyref>
  </xs:element>
</xs:schema>

Save the file with Ctrl+S.

Step 8

Right-Click on the DataModel.xsd in the Solution Explorer and click Copy. Now right-click on the Controllers folder and click Paste.

Step 9

Give each of the schema files a custom tool to execute. Select the DataModel.xsd in the root directory and enter ServerDataModelGenerator as the Custom Tool:

Select the DataModel.xsd in the Controllers folder and enter RestApiGenerator as the Custom Tool. Your project should now look like this:

Clone this wiki locally