Use the Microsoft Database First to an Existing Database with the SAP HANA Data Provider Tutorial

You can use the Microsoft Existing Database tutorial (based on the Microsoft .NET data provider) with the SAP HANA data provider. There are differences between the two providers that you must account for when following the tutorial.

Prerequisites

You must have Microsoft Visual Studio and the Microsoft .NET Framework installed on your computer.

Context

The Microsoft Database First to an Existing Database tutorial is designed for use with the Microsoft .NET data provider. The steps below provide guidance for using the SAP HANA data provider instead. Review these steps before attempting the tutorial.

Procedure

  1. For Microsoft Entity Framework 6, make sure that the data provider that supports this version is installed. Make sure there are no running instances of Microsoft Visual Studio. At a command prompt with administrator privileges, do the following:
    cd "c:\Program Files\sap\hdbclient\ado.net\v4.5"
    SetupNet.v4.5.exe /i /hanalocation "c:\Program Files\sap\hdbclient\ado.net"
  2. Before creating your Microsoft Visual Studio project, connect to the SAP HANA server and use the following SQL script to set up the Blogs, Posts, and Users tables. It is similar to the one presented in the Microsoft tutorials but uses SAP HANA SQL statements and the DEMO schema owner instead of dbo.
    CREATE SCHEMA "DEMO";
    
    CREATE COLUMN TABLE "DEMO"."Blogs" (
        "BlogId" INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
        "Name" NVARCHAR (200) NULL,
        "Url"  NVARCHAR (200) NULL
    );
    
    CREATE COLUMN TABLE "DEMO"."Posts" (
        "PostId" INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
        "Title" NVARCHAR (200) NULL,
        "Content" NTEXT NULL,
        "BlogId" INTEGER NOT NULL
    );
    
    ALTER TABLE "DEMO"."Posts"
        ADD CONSTRAINT "FK_BlogId"
        FOREIGN KEY ("BlogId") REFERENCES "DEMO"."Blogs"("BlogId");
    
    INSERT INTO "DEMO"."Blogs" ("Name","Url")
    VALUES ('SAP HANA and In-Memory Computing', 'http://scn.sap.com/community/hana-in-memory');
    
    INSERT INTO "DEMO"."Blogs" ("Name","Url")
    VALUES ('SAP Business Trends', 'http://scn.sap.com/community/business-trends');
    
    COMMIT;
    
    CREATE TABLE "DEMO"."Users"
    (
        "Username" NVARCHAR(50) NOT NULL PRIMARY KEY,
        "DisplayName" NVARCHAR(50) NULL
    );
  3. Start Microsoft Visual Studio. The steps that follow were performed successfully using Microsoft Visual Studio 2013 and Microsoft Entity Framework 6.1.3.
  4. In the Microsoft Visual Studio Server Explorer, use the Microsoft .NET Framework Data Provider for SAP HANA to create a data connection. Select a suitable Data source. For example, set Host name to your SAP HANA server and port (hana-server:30015). Fill in the User ID and Password fields. The Test Connection button is used to ensure that a connection can be made to the database. Click OK when you are done.
  5. Immediately after creating your new project, install the latest version of Microsoft Entity Framework 6 into the project, using the Nuget Package Manager.
    This step creates an App.config file that is not suitable for use with the data provider.
  6. Replace the contents of App.config with the following.
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="Sap.Data.Hana.HanaConnectionFactory, Sap.Data.Hana.v4.5, Version=1.0.100.0, Culture=neutral, PublicKeyToken=0326b8ea63db4bc4">
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="Sap.Data.Hana" type="Sap.Data.Hana.HanaProviderServices, Sap.Data.Hana.v4.5, Version=1.0.100.0, Culture=neutral, PublicKeyToken=0326b8ea63db4bc4" />
        </providers>
      </entityFramework>
    </configuration>
  7. Update all occurrences of the data provider version number in App.config. The version number should match the version of the data provider that you have currently installed.
  8. Once you have updated App.config, you must build your project. This must be done before the Reverse Engineer Model step.
  9. In the Microsoft Entity Data Model Wizard, select the option to include sensitive data in the connection string.
  10. Rather than select all tables, expand DEMO in Tables and select the Blogs and Posts tables.
  11. Continue with the instructions in the remaining steps of the tutorial.
    If you used the Update Wizard to add the Users table, rebuild the project to see the changes to the model.

Results

You have built an Microsoft Entity Framework application that uses the Database First approach for tables that already exist in the database.