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