Data Management Global

 View Only

Getting started with IBM Data Server provider for Entity Framework Core using Visual Studio 2015 update 3

By Michelle Betbadal posted Wed April 29, 2020 03:31 PM

  

[The blog refers to IBM Data Server provider for EntityFramework Core 1.1]

 

This sample core console application demonstrates, how to perform DML operation on table via IBM Data Server provider for EntityFramework Core using Visual Studio 2015 update 3 against DB2 for z/OS and DB2 for Linux, Unix, Windows database server.

 


 

Prerequisite :

  • Visual Studio 2015 update 3

  • .Net Core 1.0.1 - Visual Studio 2015 tooling

These can be downloaded from here

 


 

DDL used in EF core console application:

CREATE TABLE MYTABLE (
        ID INTEGER NOT NULL ,
        NAME VARCHAR ( 35 ) ,
        MOBILENO VARCHAR ( 15 ) ,
        ADDRESS VARCHAR ( 50 ) ,
        EMAIL VARCHAR ( 40 ) ,
CONSTRAINT  MYTABLE_ID_PK PRIMARY KEY (ID))

 



Create core console application:

Open VS 2015 update 3, and create a core console project named “MyCoreConsoleApp”.
  • From the File menu, select New > Project.
  • Select the Console Application (.Net Core) project template.
  • Enter  MyCoreConsoleApp as the name and Click on OK

    Install IBM Data Server provider for EntityFramework Core :
    You can install IBM Data Server provider for EntityFramework Core from Nuget. 
    For windows application, search for "
    IBM.EntityFrameworkCore"
    For Linux application, search for "IBM.EntityFrameworkCore-lnx"

    Create entity data model:
    Next step is, to create data model class for MyCoreConsoleApp. We will start with creating entity class that is MyTable.cs
    Create one folder named as Model. In the Model folder add one class named as MyTable.cs and replace template code with the following code.

    MyTable.cs:

    //MyTable.cs contains mapping for all columns of MyTable to .Net type:

    using System.ComponentModel.DataAnnotations;

    namespace MyCoreConsoleApp.Model
    {
        public class MyTable
        {
            [Key]
            public int ID { get; set; }
            public string NAME { get; set; }
            public string MOBILENO { get; set; }
            public string ADDRESS { get; set; }
            public string EMAIL { get; set; }
            
        }
    }

     

    Create database context class

    In the Model folder add one class named as MyContext.cs and replace template code with the following code.

     

    MyContext.cs:

    //MyContext.cs contains configuration for your model using DbContext class. It will have mapping between MyTable.cs class and MyTable table via DbSet<>.

    //Connection string must be provided to connect to database. Change <servername>,<port>, <dbname>, <uid> and <pwd> based on your Database server connection detail.

     

    using IBM.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;

    using IBM.EntityFrameworkCore.Storage.Internal;

     

    namespace MyCoreConsoleApp.Model
    {
        public class MyContext : DbContext
        {
            public MyContext() : base() { }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {     

                 optionsBuilder.UseDb2(@"Server=<servername>:<portno>;Database=<dbname>;userid=<uid>;password=<pwd>);

                 optionsBuilder.UseDb2(@"Server=<servername>:<portno>;Database=<dbname>;userid=<uid>;password=<pwd>", p=>p.SetServerInfo( <Platform Type>, <Server Version> ));

            }

            public virtual DbSet<MyTable> MYTABLE { get; set;}
        }
    }

     

    Important: IBM Data Server provider for EF Core 1.1.1 release on-wards, additional connection information has to be provided along with the connection string as shown above. More details can be found here

     

    Add code to perform DML operation

    Program.cs:

    // This file contains LINQ queries to insert, select, update and delete a row into table.

    using System;
    using System.Linq;

    namespace MyCoreConsoleApp
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Model.MyContext context = new Model.MyContext();
                //inserting record
                insertRecord_MyTable(context);
                fetchRecords_MyTable(context);
                //updating record
                updateRecord_MyTable(context);
                fetchRecords_MyTable(context);
                //deleting record
                deleteRecord_MyTable(context);
                fetchRecords_MyTable(context);  
            }
            public static void insertRecord_MyTable(Model.MyContext context)
            {
                Model.MyTable newRecord = new Model.MyTable
                {
                    ID = 101,
                    NAME = "Tom",
                    MOBILENO = "22222222",
                    ADDRESS = "37,Columbia Street,China Town",
                    EMAIL = "tom@XXX.com"
                };
                context.Add(newRecord);
                context.SaveChanges();
                
                Console.WriteLine("\nRecord inserted into MyTable");
                Console.WriteLine("****************************");
            }
            public static void updateRecord_MyTable(Model.MyContext context)
            {
                Model.MyTable updateRecord = context.MYTABLE.First(c => c.ID == 101);
                updateRecord.MOBILENO = "8989898988";
                updateRecord.ADDRESS = "100, C.V. Nagar, Bangalore";
                context.SaveChanges();
                updateRecord.EMAIL = "tom12@XXX.com";
                Console.WriteLine("\nRecord updated into MyTable");
                Console.WriteLine("***************************");
            }
            public static void deleteRecord_MyTable(Model.MyContext context)
            {
                Model.MyTable delRecord =context.MYTABLE.First(c => c.ID == 101);
                context.MYTABLE.Remove(delRecord);
                context.SaveChanges();
                Console.WriteLine("\nRecord deleted from MyTable");
                Console.WriteLine("***************************");
            }
            public static void fetchRecords_MyTable(Model.MyContext context)
            {
                Console.WriteLine("\nMyTable Detail:");
                Console.WriteLine("---------------");
                var results = context.MYTABLE.Where(c => c.ID == 101);
                foreach (var record in results )
                {
                    Console.WriteLine(" ID: " + record.ID + "\n NAME: " + record.NAME
                        + "\n MBILE NO: "+ record.MOBILENO +
                        "\n ADDRESS: " + record.ADDRESS + "\n EMAIL: "+ record.EMAIL);

                }
                if (results.Count() == 0)
                    Console.WriteLine("Record does not exist\n");
            }
        }
    }

     

    Next Step:

    Our application is ready, but before running it, we would require to set path to locate IBM CLI driver. IBM CLI driver will be installed automatically, when you install IBM Data Server provider for EntityFramework Core package. By default IBM CLI driver will be located at <nuget package install dir>\<version>\build\clidriver\bin:

    On Windows, modify PATH environment variable to contain %userprofile%\.nuget\packages\IBM.Data.DB2.Core\<version>\build\clidriver\bin

    Relaunch VS 2015 update 3 to reflect PATH environment variable settings, and Run application.

     

    Running application on Linux

    Below changes are required to execute this application on Linux:

    1. Un-install IBM.EntityFrameworkCore package and install IBM.EntityFrameworkCore-lnx package.

    2. Set below paths, before executing application:

    export DB2_CLI_DRIVER_INSTALL_PATH=$HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/<version>/build/clidriver/
    export LD_LIBRARY_PATH=
    $HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/<version>/build/clidriver/lib:$HOMEt/.nuget/packages/IBM.Data.DB2.Core-lnx/<version>/build/clidriver/lib/icc
    export PATH=
    $HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/<version>/build/clidriver/bin:$PATH

     

    Tip: Connecting against DB2 for z/OS server, would require to place license file under :

    On Windows: %userprofile%/.nuget/packages/IBM.Data.DB2.Core/<version>/build/clidriver/license

    On Linux: $HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/<version>/build/clidriver/license

     


     

    Output:

     


    <Edited the blog to update the mandatory server type changes from IBM Data Server Provider for EFCore 1.1.1 release.>
    For more information about server type setting, please go through this link 


#DataManagementGlobal
#DataServerDrivers
2 comments
175 views

Permalink

Comments

Fri June 23, 2023 09:25 PM

This is a .NET Core application. how could you use .NET Framework 4.5 to create the application?

Bruce

Fri June 23, 2023 09:20 PM

Hi Michelle,

When you created the console application, were you using .NET Framework 4.5?

Thanks for any response

Bruce