Wednesday, June 24, 2015

Entity Framework: Running Database Migrations without Visual Studio

When we are using Entity Framework, let’s say you want to run a database migrations on a machine which has no Visual Studio installed. In that kind of a scenario, you have two options.
  • Using SQL Script
  • Using migrate.exe
To show how these can be used, I am creating a console application and there I have the following.
public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
public class MyDbContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}
Now I have enabled the migrations and added an initial migration and updated the database. Now I am adding a new property to Employee class.
public class Employee
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime Birthday { get; set; }
}
Now and I am adding a new migration named “AddedBirthdayToEmployee” and still I have not updated the database.


Using SQL Script

From the development machine which has Visual Studio installed, in the Package Manager Console, we can run the Update-Database command specifying  the –Script argument, so this time the changes will be written to a script rather than applied. Additionally we can specify the SourceMigration and the TargetMigration.
Update-Database -Script -SourceMigration: sourceMigrationName -TargetMigration: targetMigrationName
When you don’t specify a source migration, EF will use the last applied migration and when you don’t specify a target migration, EF will use the latest migration which has been created. For the demo, let’s just run Update-Database –Script command.

image
Result
And I will be prompted with a SQL script.

image
Migration Script
I can save this file, and run this script on the Database itself, so the database will get migrated.

image
Database Updated


Using migrate.exe

The next option is to use migrate.exe. You can find this tool inside {project}\packages\EntityFramework.{version}\tools. Copy migrate.exe to the folder where you have the assemblies. Now from the command prompt navigate to the folder where you have the assemblies along with migrate.exe and run the following command to see all the options of migrate.exe.
Migrate.exe /?
image
migrate.exe Options
You can see you a lot of options there. For the demo, I will just write execute the following command specifying the target connectionString and the connectionProviderName. Please note that /connectionString and /connectionProviderName must be specified together.
Migrate.exe EFMigrationsDemo.exe /connectionString="Data Source=.;Initial Catalog=EFMigrationsDemo.MyDbContext;Integrated Security=True" /connectionProviderName="System.Data.SqlClient"
image
Migrations Applied
You can see the migrations are getting applied and database is updated.

image
Database Updated

Happy Coding.

Regards,
Jaliya

Friday, June 19, 2015

Visual C# Technical Guru - May 2015

Another month as a judge in Microsoft TechNet Guru Awards under Visual C# category.

The TechNet Guru Awards celebrate the technical articles on Microsoft TechNet.

Post in WikiNinjas Official Blog,
image
Visual C# Technical Guru - May 2015
Happy Coding.

Regards,
Jaliya

Wednesday, June 17, 2015

Wrote a TNWiki Article Spotlight at Official Blog of TechNet Wiki

It's true that Universal Windows Platform (UWP) Apps are getting more and more popular with upcoming Windows 10. But I believe Windows Presentation Foundation (WPF) is still the pioneer technology for creating enterprise applications for Windows.

In TechNet Wiki Ninja’s blog, Tuesday is the day of the week that Wiki Ninjas write a post highlighting a TechNet Wiki article. So In this week’s TNWiki Article Spotlight, I wrote a post highlighting an article about WPF and it has won the April month's TechNet Guru Awards under Windows Presentation Foundation (WPF) Category, an article from Andy ONeill titled Change Tracking.

image
TNWiki Article Spotlight - WPF: Change Tracking
Read the post on,
   TNWiki Article Spotlight - WPF: Change Tracking

Happy Coding.

Regards,
Jaliya

Wednesday, June 3, 2015

Wrote a TNWiki Article Spotlight at Official Blog of TechNet Wiki

Got the pleasure of writing a post in Wiki Ninjas - Official Blog of TechNet Wiki. The title of the post was TNWiki Article Spotlight - Local Files.

In TechNet Wiki Ninja’s blog, Tuesday is the day of the week that Wiki Ninjas write a post highlighting a TechNet Wiki article. In this week I wrote a post about the article that won the April month's TechNet Guru Awards under Visual C# Category, an article from Andy ONeill titled Local Files.

image
TNWiki Article Spotlight - Local Files
Read the post on,
   TNWiki Article Spotlight - Local Files

Happy Coding.

Regards,
Jaliya

Sunday, May 31, 2015

Session : Getting Started Developing Universal Windows Platform (UWP) Apps at Microsoft Imagine Windows 10 Game Jam, Sri Lanka

Yesterday delivered a two hour session titled “Getting Started Developing Universal Windows Platform (UWP) Apps” at “Microsoft Imagine Windows 10 Game Jam, Sri Lanka”.

Started off with the session giving an introduction to what’s new for developers in Microsoft developer ecosystem. Then after explaining Universal Windows Platform, Universal Device Family, Child Device Families and how their versioning works, demoed the “Hello World” UWP app. After that talked about Extension SDKs and how we isolate device specific code using Windows.Foundation.Metadata.ApiInformation static class.

Winded up the session demoing two new XAML controls which was introduced with Windows 10 which are SplitView and RelativePanel.


Happy Coding.

Regards,
Jaliya

Tuesday, May 26, 2015

Web API Resource Owner Password Flow : Returning Additional Properties along with Access Token

Web API uses the Resource Owner Password Flow defined in OAuth2. This is a quick post on how you can return additional properties when access token is requested by calling the token endpoint with a "grant_type" of "password".

When you created a ASP.NET Web API Project, insider the project, under “Providers” folder there will be a class named “ApplicationOAuthProvider”. As you know, the method which gets called when token endpoint is called, is “GrantResourceOwnerCredentials” and it returns a Task. So this is how you can include additional properties about the resource owner along with the access token. Simply add the values to AuthenticationProperties dictionary to store them as state values about the authenticated session.
ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
 
AuthenticationProperties properties = new AuthenticationProperties();
properties.Dictionary.Add("UserName", "jaliya.udagedara");
properties.Dictionary.Add("UserId", "1");
properties.Dictionary.Add("TwoFactorAuthenticationEnabled", "True");
 
AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
context.Validated(ticket);

So this is how the result looks like.
image
Result
Hope this helps.

Happy Coding.

Regards,
Jaliya

Monday, May 18, 2015

Visual C# Technical Guru - April 2015

Another month as a judge in Microsoft TechNet Guru Awards under Visual C# category.

The TechNet Guru Awards celebrate the technical articles on Microsoft TechNet.

Post in WikiNinjas Official Blog,
image
Visual C# Technical Guru - April 2015
Happy Coding.

Regards,
Jaliya

Sunday, May 10, 2015

Debugging Universal Windows Platform (UWP) Apps Without Having Windows 10 Installed in Your Physical Machine

If you are not running on Windows 10 Preview in your physical machine and if you have started developing Universal Windows Platform (UWP) Apps with Visual Studio 2015 RC, you must be feeling worried that you can only debug your applications on phone emulators running Windows 10.
Well you don’t have to worry anymore, there is one thing that you can do. That is debug on a remote device. For that,
  • The remote device and the Visual Studio computer must be connected over a network or connected directly through an Ethernet cable. Debugging over the internet is not supported.
  • The remote device must be running the remote debugging components.
  • You must be an administrator to install the remote tools on the remote device. To communicate with the remote tools, you must have user access to the remote device.
The simple way to achieve this, is by setting up a virtual machine running Windows 10 Insider Preview Build 10074 and install Remote Tools for Visual Studio 2015 RC which can be downloaded from here.

The steps to try it out is fairly simple.

Once Remote Tools for Visual Studio 2015 RC is installed Visual Studio 2015 RC Remote Debugger can be found on “C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger\{version}\msvsmon.exe”. Now start the Visual Studio 2015 RC Remote Debugger on the virtual machine.

Image1
Visual Studio 2015 RC Remote Debugger
Now make sure you can ping from host to guest.

Then from Visual Studio change your debug target to Remote Machine and you will be prompted to enter some information for the remote connection.

Image2
Visual Studio Change Debug Taret
Image3
Remote Connection Information
Enter the IP Address of the virtual machine, click on Select and then start debugging. In the Output Window of Visual Studio you can see the status of the deployment and on the virtual machines’ Visual Studio 2015 RC Remote Debugger, you can see the connection status.

Image4
Visual Studio 2015 RC Remote Debugger
And finally the application is deployed to the virtual machine.

Image5
Application Deployed to Remote
Happy Coding.

Regards,
Jaliya

Thursday, April 30, 2015

Register to attend the Microsoft MVP Virtual Conference

Hi All – I wanted to let you know about a great free event that Microsoft and the MVPs are putting on, May 14th & 15th.  Join Microsoft MVPs from the Americas’ region as they share their knowledge and real-world expertise during a free event, the MVP Virtual Conference.

The MVP Virtual Conference will showcase 95 sessions of content for IT Pros, Developers and Consumer experts designed to help you navigate life in a mobile-first, cloud-first world.  Microsoft’s Corporate Vice President of Developer Platform, Steve Guggenheimer, will be on hand to deliver the opening Key Note Address.

Why attend MVP V-Conf? The conference will have 5 tracks, IT Pro English, Dev English, Consumer English, Portuguese mixed sessions & Spanish mixed sessions, there is something for everyone! Learn from the best and brightest MVPs in the tech world today and develop some great skills!

Be sure to register quickly to hold your spot and tell your friends & colleagues.

The conference will be widely covered on social media, you can join the conversation by following @MVPAward and using the hashtag #MVPvConf.

Register now and feel the power of community!
Register
Register
Happy Coding.

Regards,
Jaliya

Monday, April 27, 2015

Installing Ubuntu 14.04.2 LTS (Desktop x64) on Hyper-V and Configure it to Connect to Internet

I had a really hard time installing Ubuntu 14.04.2 LTS (Desktop x64) on Hyper-V and configuring it to connect to Internet. Of course the installation was smooth, but configuring the Ubuntu to have Internet was a disaster for me. Have read many articles, tried different scenarios and got myself into a mess  with multiple virtual Internal and External switches. Started to hate Hyper-V, but apparently, things went well.

And this is a step by step guide for you to start from scratch. Please not that, I am not going to go in detail about creating a virtual machine on Hyper-V.

I will be explaining the process with External and Internal virtual switches.

External Virtual Switch

My environment is Windows 8.1 with a working Wi-Fi connection. No previously created Internal or External virtual switches.

The version of Ubuntu I am going to install on Hyper-V is, Ubuntu 14.04.2 LTS (Desktop x64).

In New Virtual Machine wizard, in Configure Networking, I am selecting Not Connected.

9
New Virtual Machine Wizard
2
Installation Summary
Now do the installation, in the middle of installation, you can see that you don’t have internet. That’s of course obvious.

5
Not connected to Internet
Once installed, for worse case, shut down the newly created Ubuntu virtual machine. (You actually don’t have to shut it down and you still can move ahead with below, but let’s just shut it down.)

In Hyper-V click on Virtual Switch Manager.

image
Virtual Switch Manager
There, create a new External virtual network switch.

8
New External Virtual Switch
In the next step, give a name, I am using External Virtual Switch, and I am selecting my Wi-Fi adapter to be used in virtual switch and allowing sharing as below.

6
External Virtual Switch Properties
Click on Apply and Ok.
10
Network Connections
Once it got created, Right click on vEthernet (External Virtual Switch) connection, click on Properties, go to Sharing tab. Please note that I am not doing any changes here, and I am keeping it as it is. In some articles, they say that you need to tick the checkboxes and do the allowing. There is a scenario, which you really need to enable these, and I am describing that later in the post. Hang on!. So for now, I am keeping this as it is.

15
vEthernet (External Virtual Switch) Sharing
Right click on Ubuntu Virtual Machine and click on Settings. It will look like below.

11
Virtual Machine Settings
Now set the Virtual Switch property to created External Virtual Switch.

12
Virtual Machine Settings
Click on Apply and Ok.

Now start the Ubuntu virtual machine back again and now you should have internet.

Now let’s see what are the Wired Connection properties in your Ubuntu machine. You should be seeing the following. In IPv4 Settings tab, Method is Automatic(DHCP).

17
Wired Connection IPv4 Settings
And when you check Connection Information for Wired Connection, you can see that a IP address is assigned, and the Gateway(Route) and DNS is assigned with your routers settings.

14
Wired Connection Information
Now let’s say, you still don’t have internet for some reason. Here is a workaround.

First go to vEthernet (External Virtual Switch) properties and do the allowing as below.

7
vEthernet (External Virtual Switch) Sharing
Run a ipconfig on command prompt and get the vEthernet (External Virtual Switch) values.

18
ipconfig
In Ubuntu machine, let’s manually configure the IPv4 settings as follows. And of course, you need to replace these with yours.

16
Wired Connection IPv4 Settings
Gateway is the IPv4 address of vEthernet (External Virtual Switch) or you can use the Default Gateway of vEthernet (External Virtual Switch). When you use the IPv4 address of vEthernet (External Virtual Switch), that means you are going out through vEthernet (External Virtual Switch), so you should do the allow sharing on vEthernet (External Virtual Switch) connection. When you use the Default Gateway of vEthernet (External Virtual Switch), that means you are going out through the router and not using the vEthernet (External Virtual Switch). And then of course, you don’t have to do the allow sharing on vEthernet (External Virtual Switch) connection on Windows. So the rule here is, if you are going to use the IPv4 address of vEthernet (External Virtual Switch) as your Gateway here, you need to do the allow sharing on vEthernet (External Virtual Switch) connection on Windows. Here since, I have done the allowing, I am using the IPv4 address of vEthernet (External Virtual Switch) as the Gateway.

For DNS, I am setting as 8.8.8.8 or it can be the same as the DNS of vEthernet (External Virtual Switch) which is 192.168.0.254 in my scenario.

13
Wired Connection Information
So, that’s it. Now you should have Internet.

Internal Virtual Switch

So this is how you can configure the Ubuntu virtual machine with a Internal switch. For this you will have to shutdown the Ubuntu virtual machine and do the following.

Create a new Internal Virtual Switch in Hyper-V. I am naming it as Internal Virtual Switch.

1
New Internal Virtual Switch
Click on Apply and OK.

Open up properties of vEthernet (External Virtual Switch) and do the allow sharing with newly created vEthernet (Internal Virtual Switch).

5
vEthernet (External Virtual Switch) Properties
Open up the properties of vEthernet (Internal Virtual Switch) and inside Internet Protocol Version 4 (TCP/IPv4), make sure the IP Address to look something like below. When I allow sharing of vEthernet (External Virtual Switch) with newly created vEthernet (Internal Virtual Switch), for me the default IP Address of vEthernet (Internal Virtual Switch) was in a different network range. So I have changed it as follows.

3
vEthernet (Internal Virtual Switch) TCP/IPv4 Settings
Change your Ubuntu virtual machines' Network Adapter to Internal Virtual Switch.

2
Ubuntu Virtual Machine Network Adapter
Switch on the Ubuntu virtual machine. Make you’re your connection properties look like below. If Automatic(DHCP) doesn’t give like below, you will have to assign IPv4 Settings manually. For me it was not, so I am in the Manual mode.

4
Wired Connection Properties
Give an IP address, and here the Gateway should be the IP Address of vEthernet (Internal Virtual Switch). DNS Servers can be either, public DNS which is 8.8.8.8 or the IP Address of vEthernet (Internal Virtual Switch) which is 192.168.0.100 for me. So my wired connection summary is like below.

6
Wired Connection Information
Now you should have Internet.

Please note that, sometimes, you will need to disconnect and reconnect the Wired Connection for changes to be effected. Once you got the concept, it’s possible for you to rearrange the above steps to match your need. And of course, rather than starting fresh with no previous Internal/External switches created,  you can start the whole process with previously created Internal/External switches.

Last note, Hyper-V rocks!

Your comments are mostly welcome.

Happy Coding.

Regards,
Jaliya