Data Management Global

 View Only

Getting started with ADO .NET Core MVC Web Application to access IBM Data Servers

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

  

This blog will help you to get started on ADO .NET Core MVC Web Application to connect and perform DML operations against IBM Data Servers.

 

Prerequisites:

 

  1.  Visual Studio 2017 with .Net Core 2.0
  2.  You need to have the 'Blog' table created. The script for creating and inserting data into the table is  provided towards the end of the blog. 

    Steps to follow to create an ADO .NET Core MVC Web Application to access IBM Data Servers:

     

    1. Open Visual Studio 2017 and select File->New->Project and choose 'Web' under templates and select 'ASP .NET Core Web Application' and give a name to your project say 'demoNetCoreIBMDB'  and click on OK

     



    2. In the next screen, from the ASP .NET Core Templates choose '
    Web Application(Model-View-Controller)'  , change the authentication to 'No Authentication' and click on OK



    3. Change the application target platform from 'ANYCPU' to 'x64' as shown below in the screen shots





    4. Right Click on the project and select 'Edit demoNetCoreIBMDB.csproj' and add the following PackageReferences under 'ItemGroup' element in the csproj

               <PackageReference Include="IBM.Data.DB2.Core" Version="1.2.2.100" />

               <PackageReference Include="IBM.EntityFrameworkCore" Version="1.2.2.100" />



     If you are planning to run the application on Linuxthen add the following package reference in csproj:

               <PackageReference Include="IBM.Data.DB2.Core-lnx" Version="1.2.2.100" />

               <PackageReference Include="IBM.EntityFrameworkCore-lnx" Version="1.2.2.100" />

      

    5. Right click on the Models folder and  add two class files and name them as 'Blog.cs' and 'BloggingContext.cs' respectively.






    6. Open the Blog.cs file and copy paste the following content into that file :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace demoNetCoreIBMDB.Models
    {
        public class Blog
        {
            public Blog()
            {
            }
    
            public int BlogId { get; set; }
    
            public string Url { get; set; }
        }
    
    
    }

     

     7. Open the BloggingContext.cs file and copy paste the following content into that file:

    using Microsoft.EntityFrameworkCore;
    
    namespace demoNetCoreIBMDB.Models
    {
        public class BloggingContext : DbContext
        {
            public BloggingContext(DbContextOptions options)
                                     : base(options)
            { }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Blog>(entity =>
                {
                    entity.Property(e => e.Url).IsRequired();
                });
    
            }
    
    
            public virtual DbSet<Blog> Blog { get; set; }
        }
    
    }

     

    8. Right click on the Controllers folder and select Add ->Controller , and in the next screen choose MVC Controller - Empty and name it as 'BlogsController.cs'

     




    9. Open BlogsController.cs file and copy paste the below code:

    using Microsoft.AspNetCore.Mvc;
    using demoNetCoreIBMDB.Models;
    using System.Linq;
    
    namespace demoNetCoreIBMDB.Controllers
    {
        public class BlogsController : Controller
        {
            private BloggingContext _context;
    
            public BlogsController(BloggingContext context)
            {
                _context = context;
            }
    
            public IActionResult Index()
            {
                return View(_context.Blog.ToList());
            }
        }
    }

     

    10. Right click on Views folder and create a new folder named 'Blogs'



    11. Right click on 'Views/Blogs' folder and select Add->View and enter the 'View name:' as 'Index' then click on 'Add'



    12. Open the 'Views/Blogs/Index.cshtml' page and copy paste the below content:

    @model IEnumerable<demoNetCoreIBMDB.Models.Blog>
    @{
        ViewBag.Title = "Blogs";
    }
    <h2>Blogs</h2>
    <table class="table">
        <tr>
            <th>Id</th>
            <th>Url</th>
        </tr>
        @foreach (var item in Model)
            {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.BlogId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Url)
                </td>
            </tr>
        }
    </table>

     

    13. Open the Startup.cs file and

    • add the following namespace :
        using IBM.EntityFrameworkCore;
        using IBM.EntityFrameworkCore.Storage.Internal;
        using demoNetCoreIBMDB.Models;

       
    • replace the ConfigureServices method with the below code:

      public void ConfigureServices(IServiceCollection services)
                  {
                        var connection = "DATABASE=dbname;SERVER=hostname:port;UID=username;PWD=password;";
      
                     // For SSL, use the below connection string format
                     // var connection = "DATABASE=dbname;SERVER=hostname:sslport;UID=username;PWD=password;Security=SSL;";
      
                        services.AddDbContext<BloggingContext>(options => options.UseDb2(connection,p=>p.SetServerInfo(IBMDBServerType.<servertype>, IBMDBServerVersion.<serverversion>)));
                        services.AddMvc();
                  }

       
    • Edit the Configure method and update the Mvc routes to Blogs as shown below:

                  app.UseMvc(routes =>
                  {
                      routes.MapRoute(
                          name: "default",
                          template: "{controller=Blogs}/{action=Index}/{id?}" ) ;
                  });

     

    14. Now build the solution and launch the web application and navigate to Blogs page to get the following output:.

     

    Sample: http://localhost:52123/Blogs

    Blogs
    Id  Url
    1  https://console.ng.bluemix.net/docs/cfapps/starter_app_usage.html
    2  https://www.ibm.com/developerworks/community/forums/html/forum?id=11111111-0000-0000-0000-000000000467
    3  http://blogs.msdn.com/dotnet

     

    Before launching the application on Linux, set the LD_LIBRARY_PATH either to:

                                

                     <Application_Output_directory/clidriver/lib> for bin-deployment scenarios

                                                                             OR

                     $HOME/.nuget/packages/ibm.data.db2.core-lnx/1.2.2.100/build/clidriver/lib

     

    Sample Table creation script used in this example :

    •     CREATE TABLE Blog ( BlogId int NOT NULL , url nvarchar(500) NOT NULL, CONSTRAINT PK_Blog PRIMARY KEY (BlogId))
    •     INSERT INTO Blog VALUES (1,'https://console.ng.bluemix.net/docs/cfapps/starter_app_usage.html'), (2,'https://www.ibm.com/developerworks/community/forums/html/forum?id=11111111-0000-0000-0000-000000000467'), (3,'http://blogs.msdn.com/dotnet')


    For more information about server type setting, please go through this link


    To get started on developing ASP .NET Core MVC WEB Applications using IBM Data Server driver on MacOS, please refer this link.


    #DataManagementGlobal
    #DataServerDrivers
    0 comments
    136 views

    Permalink