Numerous .NET applications operate globally, with a significant portion being ASP.NET web applications. This statistic is particularly noteworthy given the substantial forthcoming changes to the .NET framework. .NET 5.0 is set to launch in November 2020, marking a transition to a unified .NET platform that supports multiple operating systems, including Windows and Linux. Importantly, applications built on .NET versions 4.8 and earlier cannot upgrade directly to the new version due to breaking changes associated with the shift to .NET Core.
This transition is crucial for the .NET ecosystem, as it allows applications to extend beyond the confines of Windows. However, it necessitates a careful refactoring of existing applications before they can leverage the benefits of this new framework. Developers have two primary options: wait for the release of .NET 5.0 to begin refactoring or start the migration process early by converting applications to .NET Core v3.1, paving the way for a smoother upgrade to .NET 5.0. This article outlines a method for migrating an ASP.NET MVC (Model-View-Controller) web application that utilizes Entity Framework 6 to ASP.NET Core with Entity Framework Core.
Project Overview
The initial step involves setting up an ASP.NET MVC application along with its requisite database server within your AWS environment. This approach allows you to run the application locally for testing purposes. Start by configuring the database using SQL Server, which operates in Amazon Relational Database Service (Amazon RDS). Amazon RDS provides a managed SQL Server experience. After defining the database, create the necessary schema and populate it with data. If you have an existing SQL Server instance, you can also import the data there, ensuring your connection string directs to that server instead of the Amazon RDS instance used in this guide.
Next, launch a legacy MVC ASP.NET web application that presents lists of bike categories and their subcategories. This legacy system employs Entity Framework 6 for data retrieval from the database.
Finally, a step-by-step guide will demonstrate how to replicate the same use case by creating a new ASP.NET Core web application, utilizing Entity Framework Core for database interactions. It is advisable to use AWS Secrets Manager for securely storing database login credentials.
Prerequisites
Before proceeding, ensure you have the following:
- An AWS Account
- An AWS user with AdministratorAccess (refer to the AWS Identity and Access Management (IAM) console for guidance)
- Access to these AWS services:
- Amazon RDS
- Amazon Simple Storage Service (Amazon S3)
- Secrets Manager
- .NET Core 3.1 SDK installed
- Microsoft Visual Studio 2017 or a later version (Visual Studio Code can serve as an alternative)
- SQL Server Management Studio to connect to the SQL Server instance
- Experience in ASP.NET application development
Setting Up the Database Server
For this walkthrough, an AWS CloudFormation template is available in the GitHub repository to create an instance of Microsoft SQL Server Express, which can be downloaded from this link.
- In the AWS CloudFormation console, select Create stack.
- Choose Template is ready under Prepare template.
- Pick Upload a template file for Template source.
- Upload SqlServerRDSFixedUidPwd.yaml and click Next.
- Name the stack SQLRDSEXStack and proceed with Next.
- Keep the remaining options at their defaults.
- Acknowledge that AWS CloudFormation may create IAM resources with custom names.
- Click Create stack.
- Once the status indicates CREATE_COMPLETE, navigate to the Outputs tab and note the SQLDatabaseEndpoint key value.
- Use the following credentials to connect via SQL Server Management Studio: User id: DBUser, Password: DBU$er2020.
Setting Up the CYCLE_STORE Database
To establish your database, complete these steps:
- Connect to the DB instance in SQL Server Management Studio using the ID and password defined earlier.
- Under File, select New.
- Choose Query with Current Connection or select New Query from the toolbar.
- Download cycle_store_schema_data.sql and execute it.
This action will create the CYCLE_STORE database with all necessary tables and data.
Validating the Legacy MVC Application
- Download the source code from the GitHub repository.
- Open AdventureWorksMVC_2013.sln and modify the database connection string in the web.config file by replacing the Data Source property value with the server name from your Amazon RDS setup.
The ASP.NET application should now load, displaying bike categories and subcategories. The following image depicts the Unicorn Bike Rentals website post-configuration. With the legacy application operational locally, you can assess the refactoring needed to convert it into a .NET Core 3.1 application. There are two main strategies to consider:
- In-place updates: Modify everything within a single codebase.
- Code migration: Create a new .NET Core solution and gradually transfer the code.
This post will focus on the second approach, as it minimizes the need for extensive scaffolding.
Creating a New MVC Core Application
To set up your new MVC Core application, follow these steps:
- Launch Visual Studio.
- From the Get Started page, select Create a New Project.
- Choose ASP.NET Core Web Application.
- Enter AdventureWorksMVCCore.Web for the Project name.
- Select your desired Location.
- Name the Solution AdventureWorksMVCCore.
- Choose Web Application (Model-View-Controller).
- Click Create, ensuring the project is set to use .NET Core 3.1.
- Select Build, then Build Solution.
- Press CTRL + Shift + B to verify the current solution builds correctly.
You should see a default ASP.NET Core startup page.
Aligning the Projects
ASP.NET Core MVC relies on a standardized folder structure, with scaffolding dependent on view files being in the Views folder and controller files in the Controllers folder. Static content should be located in the wwwroot folder, which includes JavaScript, CSS, and image files. You’ll also need to update the configuration file to reflect the same database connection string used earlier.
To begin, transfer the static content:
- In the .NET Core solution, delete all content created during the solution setup, including css, js, and lib directories, as well as the favicon.ico file.
- Move the css, favicon, and images folders from the legacy solution into the wwwroot folder of the new solution.
By following these steps, you’ll be well on your way to modernizing your application. For more insights on this topic, check out this another blog post. Additionally, if you want to dive deeper into best practices, visit this authoritative source. Lastly, if you’re interested in how companies like Amazon train their new hires, this is an excellent resource.
Leave a Reply