DEV Community

Cover image for Migrating from ASP.net to PHP Laravel
Ken Ng
Ken Ng

Posted on

Migrating from ASP.net to PHP Laravel

Recently, we had a project to migrate from ASP.NET to Laravel. During the process, we found that the information on the internet was scattered and disorganized. At times, we had to check the Laravel framework source code to truly understand what had gone wrong. Therefore, this post serves to record the issues we encountered during the migration and how we solved them.

As we were only migrating the backend language while keeping the SQL Server database, we did not want to go too deep into ASP.NET. Consequently, there is a high chance that we may have misinterpreted the ASP.NET information presented below. Please let us know if we have.

First off, there are differences in the SQL Server database schema compared to the conventional MySQL database used by Laravel.

Take the user table as an example. The default AspNetUsers table uses the column "Id" (than "id") as its primary key, which is stored as a 128-bit GUID string by default. To model the AspNetUsers table, we had to set the primary key and cast its type to a string in the user model, like so:

Models\User.php

class User extends Authenticatable
{
    public $table = "<your-schema>.AspNetUsers";
    protected $primaryKey = 'Id';
    protected $keyType = 'string';
Enter fullscreen mode Exit fullscreen mode

Without specifying the $primaryKey, you will encounter an error like the one below, as Laravel tries to inject the instance model using id.

Missing required parameter for [URI: products/{record}/edit] [Missing parameter: record].
Enter fullscreen mode Exit fullscreen mode

If without specifying the the $keyType to string, you will encounter an error like the one below.

SQLSTATE[22018]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' to data type int. (Connection: sqlsrv, SQL: select top 1 * from [<your-schema>].[AspNetUsers] where [Id] = 5637154)
Enter fullscreen mode Exit fullscreen mode

We are using SQL Server via a Docker image. To access the SQL Server, we have to set the .env and config/database.php files.

DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=XXX
DB_USERNAME=XXX
DB_PASSWORD=XXX
Enter fullscreen mode Exit fullscreen mode

config/database.php:

'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'true'),
Enter fullscreen mode Exit fullscreen mode

To test if the above settings are correct, you can try running tinker and pulling user data from the database. If that works, you are good to proceed with your next step.

This is a rather short blog, but we hope that it is useful for anyone who is looking to migrate from ASP.NET to Laravel.

Top comments (0)