Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

29
Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud Driver Package and hosting the application in Microsoft Azure App Services As Web Apps Prerequisites: 1) Visual Studio 2015 With Update 3 2) Tables Used With The Application Must Exist Before Running The Application: -> create table blogs(blogid int primary key not null, blogdescription varchar(200) , blogurl varchar(200)) -> insert into blogs values(1,'website1' , 'www.website1.COM ') -> insert into blogs values(2,'website2','www.website2.COM ') Steps with screen shots to follow for hosting a sample DB2 .NET Application on Microsoft Azure app services as web apps: 1) Launch Visual Studio 2015 and click on File->New->Project and select the templates as ''Visual C#” → “Web” and select “ASP .NET Web Application(.NET Framework)” as show in the below screen shot and click OK :

Transcript of Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Page 1: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud Driver Package and hostingthe application in Microsoft Azure App Services As Web Apps

Prerequisites:1) Visual Studio 2015 With Update 32) Tables Used With The Application Must Exist Before Running The Application:

-> create table blogs(blogid int primary key not null, blogdescription varchar(200) , blogurl varchar(200)) -> insert into blogs values(1,'website1' , 'www.website1.COM') -> insert into blogs values(2,'website2','www.website2.COM')

Steps with screen shots to follow for hosting a sample DB2 .NET Application on Microsoft Azure app services as web apps:

1) Launch Visual Studio 2015 and click on File->New->Project and select the templates as ''VisualC#” → “Web” and select “ASP .NET Web Application(.NET Framework)” as show in the below screen shot and click OK :

Page 2: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

2) In the next screen select MVC as the ASP .NET templates and click on Change Authentication and select 'No Authentication' and click OK and then again OK

Page 3: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...
Page 4: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

3) Now we get a sample application created. First lets change the application to x64 using the configuration manager. Click on the Debug → dropdown and select Configuration Manager

Page 5: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

4) In the new window click on the drop-down for 'Active solution Configuration' and select as Release and then click on the Active solution platform and select 'New' and create a new solution platform as x64 and copy the settings from Any CPU and click on OK.

Page 6: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Then click on 'Close' . Now you should be able to see the project as Release x64

Page 7: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

5) Now install the DB2 .Net CLOUD DRIVER PACKAGE into your application. Right Click on AzureWebApp1 project and click on Manage Nuget Packages You will get the Nuget Package Manager .

Page 8: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

In the next screen, go to package source drop down and select 'nuget.org', click on Browse, and searchfor “IBM.Data.DB.Provider” and click on Install and next click on OK, Now DB2 .Net cloud driver will get installed as a part of your Application.

Page 9: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

6) Right Click on the solution and Click on “Open Folder in File Explorer”

Now Click on Packages folder and navigate through IBM.Data.DB.Provider.11.1.0.4\build\clidriver\cfg folder as shown in the below screen shots

and rename the db2dsdriver.cfg.sample to db2dsdriver.cfg

Page 10: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Now Open the db2dsdriver.cfg file in any file editor, say for ex notepad , and enter all the connection details your application is using like databasename, host , port, userid, password .Highlighted in bold are the newly added values for databasename, host , port, userid and password which application is using as connection string.Example :

<configuration> <dsncollection> <dsn alias="alias1" name="DB1" host="host.zoserver.com" port="446"/> <!-- Long aliases are supported --> <dsn alias="longaliasname2" name="name2" host="server2.net1.com" port="55551"> <parameter name="Authentication" value="SERVER_ENCRYPT"/> </dsn> </dsncollection> <databases> <database name="DB1" host="host.zoserver.com" port="50000"> <parameter name="CurrentSchema" value="DBUSER"/> <parameter name="UserId" value="DBUSER"/> <parameter name="Password" value="DBPASSWORD"/> <wlb> <parameter name="enableWLB" value="true"/> <parameter name="maxTransports" value="50"/> </wlb> <acr> <parameter name="enableACR" value="true"/> </acr> </database> <!-- Local IPC connection --> <database name="name3" host="localhost" port="0"> <parameter name="IPCInstance" value="DB2"/> <parameter name="CommProtocol" value="IPC"/> </database> </databases> <parameters> <parameter name="GlobalParam" value="Value"/> <!-- Client configuration for Optim Performance Manager Extended Insight (OPM EI) and Optim Configuration Manager (OCM)--> <!-- <parameter name="connectionSupervisorLibrary" value="/home/pureQuery/pqcmx"/> --> <!-- <parameter name="connectionSupervisorProperties" value="controllerURL=server1.net1.com:65000,httpControllerURL=http://ibmdatacmserver:12206,cmxUUID=myuuid"/> --> </parameters></configuration>

For more information about db2dsdriver.cfg configuration file, please go through the below link:http://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.swg.im.dbclient.config.doc/doc/c0054555.html

Page 11: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

7) Now come back to your application and Click on Web.Config file

Add the DbProviderFactories entry in the Web.Config which is needed for ASP .NET MVC EF 6 APP to detect ADO .Net Provider and save the file

“ <system.data> <DbProviderFactories> <add name="IBM DB2 .NET Data Provider" invariant="IBM.Data.DB2" description="IBM DB2 DataProvider for .NET Framework 4.0" type="IBM.Data.DB2.DB2Factory, IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208"/> </DbProviderFactories> </system.data>

8) Now Add the connection string entry in your web.config which will use the db2dsdriver.cfg file . Please note that in the below connection string, we have given “database=alias1” , here alias1 is the dsn name given in the db2dsdriver.cfg file as shown previously in the document.

<connectionStrings> <add name="BloggingContext" connectionString="database=alias1" providerName="IBM.Data.DB2" /> </connectionStrings>

Page 12: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

9) This step is required only for creating this demo application since it uses Entityframework6. Applications which do not use EntityFramework 6 can skip this step

Go to Nuget Package Manager and select the Package Source as “nuget.org” .Now click on Browse and enter “EntityFramework.IBM.DB2” in the search text box , you will see EntityFramework.IBM.DB2 package , click on install and accept the license agreement and IBM DB2 EF6 package is installed in your Application.

Page 13: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

10) Now the Application is done with the required setup. We have to start writing the Application business logic .

Go to AzureWebApp1 Project and Right Click on Models->Add->New Item and Go to Visual C# Code template and click on Class file and name it as 'Blog.cs'

Page 14: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Now Copy paste the below code into Blog.cs file , here make sure that the userid/schema name under which BLOGS table was created must be used in Table annotation i.e [Table("DBUSER.BLOGS")] , DBUSER is the schema under which BLOGS table was created.

using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;

namespace AzureWebApp1.Models{ [Table("DBUSER.BLOGS")] public class Blog {

public Blog() { }

[Key] [Required] public int BlogId { get; set; }

[Required] public string BlogDescription { get; set; }

[Required] public string Url { get; set; } }}

Page 15: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

11) Similarly Add another class file under Models and name it as BloggingContext.cs file

Page 16: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

and copy paste the below code into BloggingContext.cs file

using System.Data.Entity;

namespace AzureWebApp1.Models{

public class BloggingContext : DbContext {

public BloggingContext() : base("name=BloggingContext") {

} public DbSet<Blog> Blogs { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder) {

modelBuilder.Entity<Blog>() .Property(e => e.BlogId).IsRequired() .HasColumnName("BlogId");

modelBuilder.Entity<Blog>().Property(e => e.BlogDescription).IsRequired().HasColumnName("BlogDescription");

modelBuilder.Entity<Blog>() .Property(e => e.Url).IsRequired() .HasColumnName("BlogUrl");

}

}}

Page 17: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

12) Go to AzureWebApp1 Project and RightClick on Controllers-> Add → Controller..and add a MVC 5 Controller Empty → give the name of the controller as “BlogsController”

Page 18: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Open the BlogsController.cs file and replace the entire code with the below mentioned contents:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;using AzureWebApp1.Models;using System.Data.Entity.Infrastructure;

namespace AzureWebApp1.Controllers{ public class BlogsController : Controller { private BloggingContext db = new BloggingContext(); public ActionResult Index() { var s1 = from s in db.Blogs select s;

var s2 = s1.ToList();

return View(s2); }

}}

Page 19: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

13) Go to AzureWebApp1 project → Views → Blogs → Right Click and select Add → MVC 5 view Page(Razor) and enter the view name as 'Index' and Click OK .

Page 20: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Now Copy Paste the below Code into Index.cshtml file:

@model IEnumerable<AzureWebApp1.Models.Blog>

@{ ViewBag.Title = "Blogs";}

<h1>LIST OF BLOGS</h1><!DOCTYPE html>

<html><head> <meta name="viewport" content="width=device-width" /> <title>LIST OF BLOGS</title></head><body> <div> <table class="table"> <tr> <th>BlogId</th> <th>Blogdesc</th> <th>BlogUrl</th> </tr>

@foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.BlogId) </td> <td> @Html.DisplayFor(modelItem => item.BlogDescription) </td> <td> @Html.DisplayFor(modelItem => item.Url) </td> </tr> } </table>

</div></body></html>

Page 21: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

14) Now go to AzureWebApp1 Project → App Start → RouteConfig.cs → and edit the file contents and change the controller as Blogs

Page 22: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

15) Please note that since the application is 64bit, you need to change the Web Project settings to use 64 bit IIS express.

Go to Tools → Options and select to use 64 bit iis express as show in the below screen shot :

16) Now Clean and Build the solution and launch it locally(CTRL + F5) and verify its working.

You should see something like this :

Page 23: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

Deploying the Web Application into Microsoft Azure App Services :

1) Now Go to Build tab and click on “Publish to Microsoft Azure”

Page 24: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

2) In the next screen click on Profile and click on Microsoft Azure App Services

Page 25: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

3) Login to your Azure Subscription Account and select the subscription details, resource group and click on New

Page 26: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

4) In the next screen, select a name for the WebApp, Subscription, ResourceGroup , App Service Plan and click on Create . If you do not have any Resource Group/App Service Plan, you can create a new one , by clicking the New icon. Once you are done with selecting Resource group and App Service plan, click on Create , it will take some time, then you get a pop up asking to save the settings, click on Yes .

Page 27: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

5) In the next screen, click on “Validate Connection” and if it goes through and click on Publish.

Page 28: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

6) After you publish your application , since the application is 64bit, the application settings in Microsoft Azure App Services environment should be changed to 64 bit.

a) Login to https://portal.azure.com and login to your account. b) Now click on your application i.e AzureWebApp1OnCloud and go to Application Settings c) In Application settings, change the platform to 64 bit and click on save button as shown in the below screen shots

Page 29: Sample DB2 Asp .Net MVC EF 6 Application Using DB2 .Net Cloud ...

7) Now Close the application settings windows and go to your application in Azure and Restart it. Once the application is restarted click on Browse,

Congratulations,!!!!! the application is successfully pushed and launched in Microsoft Azure App Services.