Skip to content

Instantly share code, notes, and snippets.

@rungxanh1995
Last active October 5, 2023 13:05
Show Gist options
  • Save rungxanh1995/21188a35a68595d63be95de816c14e08 to your computer and use it in GitHub Desktop.
Save rungxanh1995/21188a35a68595d63be95de816c14e08 to your computer and use it in GitHub Desktop.

ASP.NET Core with JetBrains Rider/Visual Studio & Sql Server on Mac Intel/Apple Silicon

Video Demo

SQL Server + Jetbrains Rider for Mac (M1/Intel) - Updated for 2022 - Detailed Demo

SQL Server + Visual Studio for Mac (M1/Intel) - Updated for 2022 - Detailed Demo

Disclaimer

This is only for sharing purpose. If you encounter any issues, please kindly do your due diligence Googling them first. I'm new to this and still learning too.

⓵ Get Docker on Mac with an SqlServer Docker image

Based on a video tutorial by Valuetech Academy: https://www.youtube.com/watch?v=o5r3kFnsL-Q

  1. Download and install Docker on Mac (Intel/Apple chip version based on your Mac's architecture)

  2. Open Docker app, enter your Mac password to install helper, log in to your account

  3. (Optional) Open Docker Preferences, lower the system RAM resource to minimum (if you only need minimum system resource for this purpose) -> Click OK then wait for Docker to restart

  4. Open Terminal

    a. If Apple Silicon Mac, type and enter docker pull mcr.microsoft.com/azure-sql-edge:latest

    b. If Intel Mac, type and enter docker pull microsoft/mssql-server-linux

  5. Choose any password that suits SqlServer's requirements (at least 8 chars with lowercase, uppercase, numbers, special symbols), e.g. Docker@123. Then still in Terminal

    a. If Apple Silicon Mac, type and enter docker run -d --name ms-sql-server -e "ACCEPT_EULA=Y" -e 'SA_PASSWORD=Docker@123' -p 1433:1433 mcr.microsoft.com/azure-sql-edge:latest

    b. If Intel Mac, type and enter docker run -d --name ms-sql-server 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Docker@123' -p 1433:1433 microsoft/mssql-server-linux

    Note: If you want a different password, update the SA_PASSWORD part accordingly

-> If Docker runs it successfully, you'll see a long hash number displayed without any WARNING. Then to double check, head over to Docker > Containers / Apps, you'll see a Docker image with name mssql running with a green status.

Otherwise, please refer to the comment section of the video tutorial to find out more workarounds for M1 Mac environment.

⓶ Configure ASP.NET Core project to work with the database

Note: This should work similarly with an alternative IDE. Here I'm using JetBrains Rider, but conceptually you can work out the logic with Visual Studio for Mac too. Otherwise please watch the demo for Visual Studio I listed above

  1. Update the target framework of your ASP.NET Core project to the latest available SDK (e.g. .NET 6.0 at the time writing this gist)

  2. Update your currently installed NuGet packages to the corresponding target SDK (e.g. 6.0.x)

  3. 👉 This step 3, and step 4 below is IMPORTANT! It's the very part where you actually configure your current ASP.NET Core project to work with your Docker mssql server. Now, you would edit your appsettings.json database connection string.

    For example: say your app's database context is named ShopContext.cs. Then in appsettings.json, you would edit the connection string as follows:

    "ConnectionStrings": {
    	"ShopContext": "Server=(localdb)\\mssqllocaldb;Database=GuitarShop;Trusted_Connection=True;MultipleActiveResultSets=true"
    	/* Back up the above string somewhere, then update it as below with corresponding Database/Password info ... */
    	"ShopContext": "Server=localhost;Database=GuitarShop;User=sa;Password=Docker@123;"
    }

    ⚠️⚠️⚠️ Please note to NOT commit this appsettings.json edit to your group's repo, as it might break your group members' since you're using a Mac and they're on Windows. This workaround should be done locally on your macOS environment only.

    Also, if you chose a different password, change the Password part accordingly

  4. Open Terminal, make sure you installed the EF CLI tools already. Otherwise google install dotnet ef tools. Then in Terminal directed to the project solution directory

    a. If the sample code comes with migrations already, just perform an update on your current database. To do that, type and enter dotnet ef database update -> Wait till the console says Done.

    b. If the sample code doesn't come equipped with migrations, you could try adding them manually. To do that, type and enter dotnet ef migrations add <your_migration_name>. Then go back to step 4a above.

⓷ Run your ASP.NET Core App 🥳

This is the final step (albeit not necessarily needed for this tutorial), build and run the app. That's the whole tutorial there. Good luck!

P/S:

Additionally, please also check out this article to install the ODBC driver for SQL Server on macOS if you encounter any driver-related issues.

@dependra50
Copy link

thank you so much for sharing this really helpful.😊

@rungxanh1995
Copy link
Author

thank you so much for sharing this really helpful.😊

Thank you for referring my post 🤝

@AriyanBakhtiari
Copy link

Thanks a lot for sharing this , it was really helpful .

@rungxanh1995
Copy link
Author

Thanks a lot for sharing this , it was really helpful .

Thank you, you're welcome 🤝

@Tech-Sohail
Copy link

Hi, May be I am late, But I am getting this error message while trying to connect Docker Sql DB --

{System.Data.SqlClient.SqlException (0x80131904): Login failed for user ''. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnect…

@rungxanh1995
Copy link
Author

Hi, May be I am late, But I am getting this error message while trying to connect Docker Sql DB --

{System.Data.SqlClient.SqlException (0x80131904): Login failed for user ''. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnect…

I'm not sure if I can help much, but by the part Login failed for user '' can I wild guess there's a chance you may have missed to set a username? Probably in your connectionStrings

@natokey2
Copy link

natokey2 commented Nov 1, 2022

SqlException: Invalid object name 'ToDoList'. i have this error couse my friend make it on windos but when i want to open project from my mac its not working

@DonRyu
Copy link

DonRyu commented Apr 16, 2023

Wow thank you very much! you saved my time a lot.

@rungxanh1995
Copy link
Author

Wow thank you very much! you saved my time a lot.

I'm glad it's helpful and saving you time 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment