Connect Using the Microsoft Entity Framework Core (EF Core)
- How to install the .NET Core EF CLI
- How to create and debug an EF Core application that queries an SAP HANA database
- How to use the scaffold command to generate entity classes for pre-existing schema tables
Prerequisites
- You have completed the first 3 tutorials in this mission
- You have completed the previous tutorial on .NET in this mission
.NET is a free and open-source software framework for Microsoft Windows, Linux and Mac operating systems and is the successor to the .NET Framework. Entity Framework Core is a modern object-database mapper for .NET and can reduce data access code in an application. THe first example shown below either requires an empty database or one with the following table.
CONNECT USER2 Password2;
CREATE TABLE "Hotel" ("Id" int, "Name" VARCHAR (20), "Address" VARCHAR (60));
- Step 1
The
dotnettool command can be used to install and manage tools that extend .NET. The following are a few examples that can be run to show help, to list the local and globally installed tools, to uninstalldotnet-efif an incompatible version is installed, and to search the repository for version details of thedotnet-eftool.dotnet tool -? dotnet tool list -? dotnet tool list dotnet tool list -g dotnet tool uninstall dotnet-ef -g dotnet tool search dotnet-ef --detailThe SAP HANA Client 2.17 release supports EF Core 6.0 & 7.0. For a list versions and support dates see EF Core releases and planning and SAP Note 3165810 - SAP HANA Client Supported Platforms.
Run the following command to install version 7 of the dotnet-ef tool.
ShellCopydotnet tool install dotnet-ef --version 7.0.17 -g dotnet tool list -g
The help for the .NET Command Line Tools can be displayed as shown below.
ShellCopydotnet ef -h
- Step 2
-
Create a new console app with the below commands:
Shell (Microsoft Windows)Copycd %HOMEPATH%/HANAClientsTutorial dotnet new console -o EFCoreShell (Linux or Mac)Copycd $HOME/HANAClientsTutorial dotnet new console -o EFCore -
Open the
EFCore.csprojfile:Shell (Microsoft Windows)Copycd EFCore notepad EFCore.csprojShell (Linux or Mac)Copycd EFCore pico EFCore.csprojAdd the following below the
PropertyGroupsection (within theProjectsection) to indicate where to load the SAP HANA Client .NET and entity driver from. Modify theHintPathsection with the information about where the dlls are located on your machine.Shell (Microsoft Windows)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>C:\SAP\hdbclient\dotnetcore\v6.0\Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> <Reference Include="Sap.EntityFrameworkCore.Hana.v7.0"> <HintPath>C:\SAP\hdbclient\dotnetcore\v6.0\Sap.EntityFrameworkCore.Hana.v7.0.dll</HintPath> </Reference> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17"> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.17"> </PackageReference> </ItemGroup>Shell (Linux or Mac)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>/home/dan/sap/hdbclient/dotnetcore/v6.0/Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> <Reference Include="Sap.EntityFrameworkCore.Hana.v7.0"> <HintPath>/home/dan/sap/hdbclient/dotnetcore/v6.0/Sap.EntityFrameworkCore.Hana.v7.0.dll</HintPath> </Reference> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17"> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.17"> </PackageReference> </ItemGroup>
Once the
dotNet.csprojfile has been updated, save, and close the file. -
Run the app to validate that SAP hdbclient DLLs can be loaded:
ShellCopydotnet runThe expected output is
Hello, World!.If a warning occurs mentioning that a SAP reference could not be resolved, revisit the
EFCore.csprojfile and double check that the hintpath is correct. -
Open an editor and create a file named
HotelModel.cs.Shell (Windows)Copynotepad HotelModel.csShell (Linux or Mac)Copypico HotelModel.cs -
Copy the below code into
HotelModel.cswith the code below:C#Copyusing Microsoft.EntityFrameworkCore; using Sap.EntityFrameworkCore.Hana; public class HotelContext : DbContext { public DbSet<HotelEF> Hotel { get; set; } public HotelContext() { var folder = Environment.SpecialFolder.LocalApplicationData; var path = Environment.GetFolderPath(folder); Database.EnsureDeleted(); Database.EnsureCreated(); } protected override void OnConfiguring(DbContextOptionsBuilder options) { options.UseHana("Server=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx.hana.prod-xxxx.hanacloud.ondemand.com:443;UserName=User2;Password=Password2;CurrentSchema=User2"); } } public class HotelEF { public int Id { get; set; } = 0; public string Name { get; set; } = string.Empty; public string Address { get; set; } = string.Empty; }Be sure to update the host URL and optionally the user name and password. Note that calls to EnsureDeleted and EnsureCreated will delete and recreate the objects in the schema USER2. As documented at RelationalDatabaseCreator.EnsureDeleted Method it will delete all objects in the schema USER2.
-
Open an editor to edit the file
Program.cs.Shell (Windows)Copynotepad Program.csShell (Linux or Mac)Copypico Program.cs -
Replace the entire contents of
Program.cswith the code below. Save and close the file when finished.C#Copyusing var db = new HotelContext(); // Create Console.WriteLine("Inserting a new Hotel"); db.Add(new HotelEF { Id = 1, Name = "The Inn of Waterloo", Address = "475 King St N, Waterloo" }); db.Add(new HotelEF { Id = 2, Name = "The Walper Hotel", Address = "20 Queen St S, Kitchener" }); db.SaveChanges(); // Read Console.WriteLine("Querying for a hotel"); var hotels = db.Hotel .OrderBy(b => b.Name).Last(); Console.WriteLine("Found: " + hotels.Name);Further details on SAP HANA Client entity core driver can be found at Entity Framework Core Support. Further .NET API details can be found in the .NET API browser.
-
Run the app:
ShellCopydotnet runBefore running the program make sure to be in the directory where Program.cs is saved

-
- Step 3
-
Open Visual Studio Code. If needed, download the application here.
-
If you have not already done so, choose File | Add Folder to Workspace, and then add the
HANAClientsTutorialfolder.
-
Open the file
Program.csand set a breakpoint. -
Select Run | Start Debugging | .NET Core. A configuration will be added. Choose Run | Start Debugging.
Notice that the debug view becomes active and that the RUN option is .NET Launch.
Notice that the program stops running at the breakpoint that was set.
Observe the variable values in the leftmost pane. Step through code.

For further information on debugging .NET apps consult Tutorial: Debug a .NET Core console application using Visual Studio Code and Instructions for setting up the .NET Core debugger.
-
- Step 4
The following steps demonstrate the process of generating entity type classes and a DbContext class based on an existing database schema. Additional details can be found at Scaffolding (Reverse Engineering).
-
Create a new console app with the below commands:
Shell (Microsoft Windows)Copycd %HOMEPATH%/HANAClientsTutorial dotnet new console -o EFCoreScaffoldShell (Linux or Mac)Copycd $HOME/HANAClientsTutorial dotnet new console -o EFCoreScaffold -
Open the
EFCoreScaffold.csprojfile:Shell (Microsoft Windows)Copycd EFCoreScaffold notepad EFCoreScaffold.csprojShell (Linux or Mac)Copycd EFCoreScaffold pico EFCoreScaffold.csprojAdd the following below the
PropertyGroupsection (within theProjectsection) to indicate where to load the SAP HANA Client .NET and entity driver from. Modify theHintPathsection with the information about where the dlls are located on your machine.Shell (Microsoft Windows)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>C:\SAP\hdbclient\dotnetcore\v6.0\Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> <Reference Include="Sap.EntityFrameworkCore.Hana.v7.0"> <HintPath>C:\SAP\hdbclient\dotnetcore\v6.0\Sap.EntityFrameworkCore.Hana.v7.0.dll</HintPath> </Reference> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17"> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.17"> </PackageReference> </ItemGroup>Shell (Linux or Mac)Copy<ItemGroup> <Reference Include="Sap.Data.Hana.Core.v6.0"> <HintPath>/home/dan/sap/hdbclient/dotnetcore/v6.0/Sap.Data.Hana.Net.v6.0.dll</HintPath> </Reference> <Reference Include="Sap.EntityFrameworkCore.Hana.v7.0"> <HintPath>/home/dan/sap/hdbclient/dotnetcore/v6.0/Sap.EntityFrameworkCore.Hana.v7.0.dll</HintPath> </Reference> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17"> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.17"> </PackageReference> </ItemGroup> -
Install the required package
Microsoft.EntityFrameworkCore.Design.ShellCopydotnet add package Microsoft.EntityFrameworkCore.Design --version 7.0.17The list of installed packages can be seen using the below command.
ShellCopydotnet list package
Additional details can be found at dotnet add package and Microsoft.EntityFrameworkCore.Design
-
Use the scaffold command to generate entity classes for the HOTELS schema. Update the SQL endpoint.
ShellCopydotnet ef dbcontext scaffold "Server=xxxxxx-4782-bc7e-297099099b59.hana.prod-ca10.hanacloud.ondemand.com:443;uid=USER2;pwd=Password2;Current Schema=HOTELS" Sap.EntityFrameworkCore.Hana.v7.0 --schema HOTELS --context HotelsContext
Should you wish to regenerate the files in the future and overwrite the existing files, the
--forceparameter can be used. Additional details on the scaffold command can be found at .NET Core CLI. -
Open an editor to edit the file
Program.cs.Shell (Windows)Copynotepad Program.csShell (Linux or Mac)Copypico Program.cs -
Replace the entire contents of
Program.cswith the code below. Save and close the file when finished.C#Copyusing EFCoreScaffold; using var db = new MyHotelsContext(true); // Create Console.WriteLine("Inserting a new maintenance item"); db.Add(new Maintenance { Mno = 3, Description = "Replace cracked mirror in lobby bathroom" }); db.SaveChanges(); // Read Console.WriteLine("Querying for a maintenance item"); var maintenanceItems = db.Maintenances .OrderBy(b => b.Hno).Last(); Console.WriteLine("Found item#: " + maintenanceItems.Mno + " Desc: " + maintenanceItems.Description); -
Open an editor to edit the file
HotelsContext.cs.Shell (Windows)Copynotepad HotelsContext.csShell (Linux or Mac)Copypico HotelsContext.cs -
Delete the
OnConfiguringmethod. This will be added to theMyHotelsContext.csclass. -
Open an editor to create and edit a new file named
MyHotelsContext.cs.Shell (Windows)Copynotepad MyHotelsContext.csShell (Linux or Mac)Copypico MyHotelsContext.cs -
Add the code below. Update the Server= line to match your SAP HANA Cloud SQL endpoint. Save and close the file when finished. Note that the schema is changed to be USER2 while the original objects are in the schema HOTELS.
C#Copyusing Microsoft.EntityFrameworkCore; using Sap.EntityFrameworkCore.Hana; namespace EFCoreScaffold; internal class MyHotelsContext : HotelsContext { public MyHotelsContext(bool createTables) : base() { if (createTables) { // Delete the existing database tables and re-create new tables. Database.EnsureDeleted(); Database.EnsureCreated(); } } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseHana("Server=xxxxxxxx-4782-bc7e-297099099b59.hana.prod-ca10.hanacloud.ondemand.com:443;uid=USER2;pwd=Password2;Current Schema=USER2"); } } -
Run the app:
ShellCopydotnet run
-
- Step 5
Congratulations! You have now created and debugged a .NET application that connects to and queries an SAP HANA database.
Which of the following statements are true?