In today’s tech landscape, numerous .NET applications are operational globally, particularly among ASP.NET web applications. This becomes increasingly significant as the .NET framework is set to undergo major transformations. The impending release of .NET 5.0 is scheduled for November 2020, which will unify the platform, allowing developers to target multiple environments like Windows and Linux. However, applications built on version 4.8 and earlier cannot seamlessly transition to this new iteration due to the foundational changes introduced in .NET 5.0, which is derived from .NET Core.
This shift is crucial for the .NET ecosystem, as it paves the way for applications to extend beyond the Windows environment. Nevertheless, existing applications require refactoring to leverage these advancements. Developers face a choice: either wait for the official release of the new .NET version and commence refactoring then, or proactively migrate to .NET Core v3.1, facilitating a smoother transition to .NET 5.0. This article outlines a process for upgrading an ASP.NET MVC (Model View Controller) web application utilizing Entity Framework 6 to an ASP.NET Core application with Entity Framework Core.
Solution Overview
The initial step involves setting up an ASP.NET MVC application along with its requisite database server in your AWS environment. This approach allows you to run the application locally and observe its functionality. Begin by configuring the database, which will be SQL Server hosted on Amazon Relational Database Service (Amazon RDS), a managed SQL Server solution. After defining the database, you’ll establish the schema and data. If you prefer, data can be loaded into your own SQL Server instance; just ensure that your connection string points to that instance instead of the Amazon RDS setup.
Next, initiate a legacy MVC ASP.NET web application showcasing lists of bike categories and their subcategories. This legacy system employs Entity Framework 6 for data retrieval from the database. The final step is a methodical migration to create a new ASP.NET Core web application. Here, you will utilize Entity Framework Core for database interactions, and it’s advisable to use AWS Secrets Manager to securely store your database login credentials.
Prerequisites
For this tutorial, ensure you meet the following prerequisites:
- An AWS Account
- An AWS user with AdministratorAccess (refer to the AWS Identity and Access Management (IAM) console for guidance)
- Access to the following AWS services: Amazon RDS, Amazon Simple Storage Service (Amazon S3), and Secrets Manager
- .NET Core 3.1 SDK installed
- Microsoft Visual Studio 2017 or later (Visual Studio Code is also an option)
- SQL Server Management Studio for database connection
- Experience in ASP.NET application development
Setting Up the Database Server
For this guide, we’ve included an AWS CloudFormation template in the GitHub repository to create a Microsoft SQL Server Express instance. You can download it from this link.
- In the AWS CloudFormation console, select Create stack.
- For Prepare template, choose Template is ready.
- For Template source, select Upload a template file.
- Upload SqlServerRDSFixedUidPwd.yaml and click Next.
- Enter SQLRDSEXStack as the Stack name and proceed.
- Leave 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 value under the SQLDatabaseEndpoint key.
- Use SQL Server Management Studio to connect to the database with these credentials: User id: DBUser Password: DBU$er2020.
Setting Up the CYCLE_STORE Database
To configure your database, follow these steps:
- Connect to the DB instance using SQL Server Management Studio with the previously defined ID and password.
- Under File, select New.
- Choose Query with Current Connection or click New Query from the toolbar.
- Download
cycle_store_schema_data.sql
and execute it.
This will create the CYCLE_STORE database complete with necessary tables and data.
Setting Up and Validating the Legacy MVC Application
- Download the source code from the GitHub repository.
- Open AdventureWorksMVC_2013.sln and update the database connection string in the web.config file by substituting the Data Source property with the server name from your Amazon RDS setup.
The ASP.NET application should display bike categories and subcategories. The following screenshot illustrates the Unicorn Bike Rentals website after configuration. Now that the legacy application is running locally, you can explore what refactoring it into a .NET Core 3.1 application entails. Two primary methods are available:
- Update in place: Make all modifications within a single codebase.
- Move code: Create a new .NET Core solution and gradually transfer code.
For this article, we will demonstrate the second approach, as it involves less scaffolding.
Creating a New MVC Core Application
To establish your new MVC Core application, perform the following:
- Open Visual Studio.
- From the Get Started page, select Create a New Project.
- Choose ASP.NET Core Web Application.
- For Project name, enter AdventureWorksMVCCore.Web.
- Select your preferred Location.
- Enter AdventureWorksMVCCore as the Solution name.
- Choose Web Application (Model-View-Controller).
- Click Create, ensuring that the project is set to use .NET Core 3.1.
- Select Build, then Build Solution.
- Press CTRL + Shift + B to confirm that the current solution is building correctly.
You should see a default ASP.NET Core startup page.
Aligning the Projects
ASP.NET Core MVC relies on a specific folder structure; much of the scaffolding is contingent on source code files being organized correctly within the Views and Controllers folders. Additionally, static content, such as JavaScript, CSS, and images, should reside within the wwwroot folder. Update the configuration file with the same database connection string you used previously.
To begin, transfer the static content:
- In the .NET Core solution, remove all content that was generated during solution creation, which includes css, js, and lib directories, along with the favicon.ico file.
- Transfer the css, favicon, and Images folders from the legacy solution to the new solution’s wwwroot folder.
For further insights, this is another blog post that dives deeper into the topic: Check it out here. Additionally, if you’re looking for authoritative information on this subject, you can visit this resource. Also, for visual learners, this video serves as an excellent resource.
Leave a Reply