Example: Use the SAP HANA Entity Framework Core Database Provider

Use the SAP HANA Entity Framework Core database provider to create and query blogs.

Prerequisites

  • You must have the Microsoft .NET 6 or later installed on your computer.

  • You can use either the .NET SDK (6 or later) or the Microsoft Visual Studio (2019 or 2022) to build the example.

Context

This example demonstrates how to use modelling, using the Blog and Post entity classes, with a context object to interact with the underlying SAP HANA database to create two new blogs, each with a new post, and then look for blogs and posts matching specific criteria.

Procedure

  1. Configure the project.

    Create a C# Console Application that targets the .NET 6 Framework. Add the following packages to the .csproj file:

    • Microsoft.EntityFrameworkCore 6

    • Microsoft.EntityFrameworkCore.Relational 6

    Ensure your .csproj file contains <"Sap.EntityFrameworkCore.Hana.v6.0" />.

    To build and run this example, import the following namespaces:

    using Microsoft.EntityFrameworkCore;
    using Sap.EntityFrameworkCore.Hana;
  2. Create the Entity Framework Core model.
    public class HanaDbContext : DbContext
    {
        public HanaDbContext(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=my-server:30015;UserName=SYSTEM;Password=password");
        }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }
    
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public int Rating { get; set; }
        public string? Description { get; set; }
        public List<Post> Posts { get; set; }
    }
    
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
  3. Create the query entities.

    Add the following code to the Main function:

    using (var context = new HanaDbContext(true))
    {
        context.AddRange(
            new Blog
            {
                Url = "https://blogs.sap.com/",
                Rating = 4,
                Description = "All Blog Posts",
                Posts = new List<Post>
                {
                    new Post {Title = "New client interface features", Content = "Posts about new features in the SAP HANA client interfaces." }
                }
            },
            new Blog
            {
                Url = "https://community.sap.com/",
                Rating = 5,
                Description = "SAP Community",
                Posts = new List<Post>
                {
                    new Post {Title = "SAP HANA client installation", Content = "Client interfaces for connecting applications as part of the SAP HANA client software package." }
                }
            });
    
        var count = context.SaveChanges();
        Console.WriteLine($"Saved {count} entities to the database.");
    
        Console.WriteLine("List blogs with Rating above 3.");
        var blogs = context.Blogs.Where(b => b.Rating > 3).ToList();
        foreach (var blog in blogs)
            Console.WriteLine($"Url = {blog.Url}, Rating = {blog.Rating}, Description = {blog.Description}");
    
        Console.WriteLine("List posts contain interfaces.");
        var posts = context.Posts.Where(p => p.Content.Contains("interfaces")).ToList();
        foreach (var post in posts)
            Console.WriteLine($"Title = {post.Title}, Content = {post.Content}");
    }
  4. Build and run the example.

    Use dotnet SDK or Visual Studio to build and run the example.