January 2018 : Instructor-led Online Course in ASP.NET Core 2.0. Conducted by Bipin Joshi. Read more...
Registration for January 2018 batch of ASP.NET Core 2.0 instructor-led online course has already started. Conducted by Bipin Joshi. Register today ! Click here for more details.

Migrating Source Code from ASP.NET Core RC2 to ASP.NET Core 1.0

If you have been reading my book Beginning SOLID Principles and Design Patterns for ASP.NET Developers then you might have noticed that the book uses RC1 of ASP.NET Core. After the book got published I updated the book's source code to RC2. Now that ASP.NET Core 1.0 is already released you might want to migrate the source code from RC2 to 1.0. To that end this article provides an outline of the steps involved in upgrading the book's source code from ASP.NET Core RC2 to ASP.NET Core 1.0.

I am going to use the Contact Manager application that has been discussed in Chapter 1 for the sake of this article. This article will illustrate the following:

  • How to migrate the Contact Manager application from RC2 to 1.0 using Visual Studio 2015
  • How to migrate the Contact Manager application from RC2 to 1.0 using Visual Studio 2017

Once you understand the steps involved in the migration, you can apply the same process to rest of the projects. Of course, at places you may need to tweak the example code to make it work on ASP.NET Core 1.0. But that should be trivial step in most of the cases.

Before you read any further keep the RC2 source code of the Contact Manager application ready with you.

Migrating Contact Manager application from RC2 to 1.0 using Visual Studio 2015

Copy the Chapter_01 folder from RC2 code to a new location. The Chapter_01 folder contains ContactManager subfolder.

Now launch Visual Studio 2015. Click on File > Open > Project / Solution menu option. Doing so will open Open Project dialog. Locate and select the Chapter 01.sln file from the Chapter_01 folder.

Click on the Open button.

Visual Studio 2015 opens the ContactManager project in the Solution Explorer.

Then open Project.json file in the IDE. In its current form it will look like this:

As you can see it references RC2 assemblies as indicated by the -rc2-final suffix. Also notice that the "Tools" use -preview1-final suffix.

Now search for all the occurrences of -rc2-final and replace them with empty string.

On the same lines replace -preview1-final with -preview2-final.

So, now your Project.json resembles this:

Save Project.json file. Then right click on the References folder and click on the Restore Packages shortcut menu option.

Doing so will add version 1.0 packages to your project.

Now build the project as you normally do. If all goes well the build will succeed.

Before you run the application open the appsettings.json file and change the database connection string to match your setup.

To create the ContactDb database and Contacts table used by the application issue the following EF migrations commands on Visual Studio 2015 command prompt:

> dotnet ef migrations add MyMigrations
> dotnet ef database update

That's it! Run the application and see if you can add and remove contacts. The following figure shows a sample run of the application after migrating the code to ASP.NET Core 1.0.

 

Migrating Contact Manager application from RC2 to 1.0 using Visual Studio 2017

Copy the Chapter_01 folder from RC2 code to a new location. The Chapter_01 folder contains ContactManager subfolder.

Now launch Visual Studio 2017. Click on File > Open > Project / Solution menu option. Doing so will open Open Project dialog. Locate and select the Chapter 01.sln file from the Chapter_01 folder.

Click on the Open button.

At this stage the Visual Studio 2017 will prompt you with the following dialog:

As you can see Visual Studio 2017 tells you that the project file is not supported and needs to be upgraded to the format used by Visual Studio 2017.

Keep defaults unchanged and click on OK button.

Visual Studio 2017 migrates the project and opens the new project in Solution Explorer as shown below.

A browser window is also opened and the migration report is shown.

Observe the Solution Explorer carefully. Did you notice something? There is no longer Project.json file. The new Visual Studio 2017 project format stores the assembly information inside the ContactManager.csproj file. If you open this file in Notepad you will see them as shown below:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" 
Version="1.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Server.
IISIntegration" Version="1.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.
Server.Kestrel" Version="1.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" 
Version="1.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.
SqlServer" Version="1.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.
Tools" Version="1.0.0">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration.
EnvironmentVariables" Version="1.0.2" />
    <PackageReference Include="Microsoft.Extensions.
Configuration.Json" Version="1.0.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.
CodeGeneration.Design" Version="1.0.0">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
</ItemGroup>

As you can see the RC2 assemblies have been changed to version 1.0.x during the upgrade. You can also confirm this by right clicking on the Dependencies node and then clicking Manage NuGet Packages.

Now build the project. If your migration is successful the project will also build successfully.

Before you run the application open the appsettings.json file and change the database connection string to match your setup.

To create the ContactDb database and Contacts table used by the application issue the following EF migrations commands on Visual Studio 2017 command prompt:

> dotnet ef migrations add MyMigrations
> dotnet ef database update

That's it! Run the application and see if you can add and remove contacts. The following figure shows a sample run of the application after migrating the code to ASP.NET Core 1.0.


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 16 June 2017