Data Management Global

 View Only

Creating Entity Data Model using IBM Data Server providers for Entity Framework Core

By Michelle Betbadal posted Wed April 29, 2020 04:40 PM

  

Creating entity and context classes for an existing IBM database is supported through IBM Data Server providers for Entity Framework Core version 1.2. It supports Database-First approach using the Scaffold-DbContext command of Package Manager Console and .NET Core CLI. This reverse engineering command creates entity and context classes based on the schema of the existing database. If we have already had a database tables in place, we don't need to hand-roll our models. The IBM Data Server Providers for Entity Framework Core packages are available for download in the NuGet repository.

 

Package name

    For Windows platform  : IBM.EntityFrameworkCore

    For Linux platform        : IBM.EntityFrameworkCore-lnx

    For MacOS platform    : IBM.EntityFrameworkCore-osx

 

Prerequisites

  • Visual Studio 2017
  • .NET Core SDK 2.0

For windows platform, there are two ways to generate models from our existing database:

Option 1: Using Package Manager Console

    1. Open Visual Studio and create a new Console App (.NET Core) for C# through

       File -> New  -> Project -> Visual C# ->  .NET Core -> Console App (.NET Core)

    2. Install the following NuGet packages by selecting Package Manager Console from the Tools -> NuGet Package Manager menu and run the below commands in package manager console window prompt.

  • Install-package Microsoft.EntityFrameworkCore.Tools -Version 2.0.0
  • Install-Package IBM.EntityFrameworkCore -Version 1.2.2.100

    3. Enter the Scaffold-DbContext command with a connection string and our provider as parameters in package manager Console window to create the entities and DbContext for the existing database tables.

      Scaffold-DbContext "server=localhost:60000;uid=user1;pwd=pass1;database=sample" IBM.EntityFrameworkCore -o sample -f -schema myschema

which will create a new sample folder inside the project, that contains all the tables mapped to entities and the sampleContext.cs file.

Option 2: Using Command Window    

    1. Create a folder for the project

mkdir SampleScaffold

    2. Then navigate to newly created folder (SampleScaffold).

          cd SampleScaffold

    3. Create .NET Core console project using the .NET Core command-line interface (CLI)

dotnet new console 

    4. Add a reference to the IBM.EntityFrameworkCore Nuget package

          dotnet add package IBM.EntityFrameworkCore -v 1.2.2.100      

    5. Add a reference to Microsoft.EntityFrameworkCore.Tools.DotNet as a DotNetCliToolReference entry in the SampleScaffold.csproj file as follows:

         <ItemGroup> 
             <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
         </ItemGroup>

    6. Restore dependencies and project-specific tools that are specified in the .csproj project file.

                    dotnet restore 

    7. To generate model use the dbcontext Scaffold command. The command has two required arguments i.e. a connection string and a provider.

dotnet ef dbcontext scaffold "server=localhost:60000;uid=user1;pwd=pass1;database=sample" IBM.EntityFrameworkCore -o  samplemodel  --schema   myschema  -f

    8. Once you have executed the command, you will see that a samplemodel folder has been created in the project folder, containing a collection of class files corresponding to all tables mapped to entities in addition to a file for the DbContext class.

 

Scaffolding a Database by Filtering Tables

    Generating entity data model for only specific tables is also possible by providing the tables filter information in scaffold command. The command-line examples that follow show the parameters needed for filtering tables.

    Using Package Manager Console:

Scaffold-DbContext "server=localhost:60000;uid=user1;pwd=pass1;database=sample" IBM.EntityFrameworkCore -o sample -schema myschema -t employees, owners -f

    Using .NET Core Command Window:

dotnet ef dbcontext scaffold "server=localhost:60000;uid=user1;pwd=pass1;database=sample" IBM.EntityFrameworkCore -o sample --schema myschema -t employees -t owners -f

    

In Linux and MacOS platforms, for scaffolding an existing database we need to use the .NET Core command line way by performing the same steps mentioned above in “Option 2: Using Command Window” section. However, in step 4, we need to use Linux Entity Framework Core package (i.e. IBM.EntityFrameworkCore-lnx) or MacOS Entity Framework Core package (i.e. IBM.EntityFrameworkCore-osx).

 

Note: In Linux platform, we need to set LD_LIBRARY_PATH to installed clidriver path location (i.e. export LD_LIBRARY_PATH=$HOME/.nuget/packages/ibm.data.db2.core-lnx/1.2.2.100/build/clidriver/lib) before executing the dbcontext scaffold command. Whereas in MacOS platform, we don’t need to set anything before executing the  dbcontext scaffold command.

 

Tip: To create entity data model against DB2 for z/OS and DB2 for i server, we need to provide license file under:

 

On Windows: %userprofile%/.nuget/packages/ibm.data.db2.core/1.2.2.100/build/clidriver/license

On Linux: $HOME/.nuget/packages/ibm.data.db2.core-lnx/1.2.2.100/build/clidriver/license

On MacOS : $HOME/.nuget/packages/ibm.data.db2.core-osx /1.2.2.100/build/clidriver/license


#DataManagementGlobal
#DataServerDrivers
2 comments
100 views

Permalink

Comments

Fri October 09, 2020 09:02 AM

I am using IBM.EntityFrameworkCore 3.1 for the Hybrid Data Management.
I am getting blocked because of some legacy Tables with Reserve DB2 words.Eg:- user, group.
1) I am running into object undefined errors if I let them be
2) Tried keeping the object names under qoutes " ". This is in turn leading to other troubles with JOIN statements
These tables cannot be renamed at the moment as there are heavy dependencies following.

Fri June 12, 2020 09:36 AM

It's possible to add information what Microsoft.EntityFrameworkCore.Tools use with IBM.EntityFrameworkCore 3.1.0.100 or 3.1.0.200?!