{"id":20625111,"url":"https://github.com/jasonsturges/mysql-dotnet-core","last_synced_at":"2025-06-20T05:38:37.531Z","repository":{"id":49732386,"uuid":"76596279","full_name":"jasonsturges/mysql-dotnet-core","owner":"jasonsturges","description":"ASP.NET Core 5.0 Web Application using MySQL with Entity Framework","archived":false,"fork":false,"pushed_at":"2022-12-29T21:04:33.000Z","size":2245,"stargazers_count":109,"open_issues_count":2,"forks_count":51,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-15T15:07:06.158Z","etag":null,"topics":["asp","aspnet","aspnet-core","coreclr","database","dotnet","dotnet-core","dotnet5","entity-framework","mysql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasonsturges.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-15T21:09:18.000Z","updated_at":"2025-01-09T14:10:19.000Z","dependencies_parsed_at":"2023-01-31T10:00:44.926Z","dependency_job_id":null,"html_url":"https://github.com/jasonsturges/mysql-dotnet-core","commit_stats":null,"previous_names":[],"tags_count":4,"template":true,"template_full_name":null,"purl":"pkg:github/jasonsturges/mysql-dotnet-core","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fmysql-dotnet-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fmysql-dotnet-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fmysql-dotnet-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fmysql-dotnet-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonsturges","download_url":"https://codeload.github.com/jasonsturges/mysql-dotnet-core/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fmysql-dotnet-core/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260890581,"owners_count":23077863,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp","aspnet","aspnet-core","coreclr","database","dotnet","dotnet-core","dotnet5","entity-framework","mysql"],"created_at":"2024-11-16T13:08:11.010Z","updated_at":"2025-06-20T05:38:32.500Z","avatar_url":"https://github.com/jasonsturges.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQL ASP.NET 5.0\n\n- _Note: .NET 7 version is in progress on branch [`dotnet-7.0`](https://github.com/jasonsturges/mysql-dotnet-core/tree/dotnet-7.0), pending resolution of issues._\n- _Note: Microsoft SQL Server (MSSQL) now runs on Mac / Linux via Docker, and may be considered as a viable cross platform solution._\n- _Note: MySQL via Pomelo data adapter is functional, and a viable alternative to Oracle's adapters used here._\n- _Note: PostgreSQL [alternative version](https://github.com/jasonsturges/postgresql-dotnet-core) is also available, with .NET 7 update complete._\n\nConvert an [ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-5.0) Web Application project to use [MySQL](https://www.mysql.com/) with [Entity Framework](https://docs.microsoft.com/en-us/ef/), enabling development on macOS, linux, or Windows targets using IDEs such as [VS Code](https://code.visualstudio.com/), [Visual Studio](https://visualstudio.microsoft.com/), or [JetBrains Rider](https://www.jetbrains.com/rider/).\n\nThis project uses [.NET 5.0](https://dotnet.microsoft.com/download/dotnet/5.0) target framework, ASP.NET Core Web Application (Model-View-Controller) project scaffold from Visual Studio 2019 (version 16.10.1) to connect to MySQL 8.0.\n\n![vscode](https://user-images.githubusercontent.com/1213591/106405974-812cba80-63fd-11eb-9c22-3f8eeff9136f.png)\n\nFor previous versions of .NET Core 3.x, 2.x, 1.x, see the [releases](https://github.com/jasonsturges/mysql-dotnet-core/releases) for past implementations in this repository.\n\n\n## Quick Start\n\nTo immediately use this solution, make sure your [environment setup](#environment-setup) is complete; then, jump to [running the solution](#running-the-solution).\n\n\n## Environment Setup\n\nMake sure you have the [.NET 5.0 SDK](https://dotnet.microsoft.com/download) installed on your system.\n\nIf you're using Visual Studio Code, you will need to generate ASP.NET Core developer certificates by issuing the following commands from a terminal:\n\n    dotnet dev-certs https --clean\n    dotnet dev-certs https\n\nFor command line `database ef` commands, you will need to install Entity Framework Core tools .NET CLI:\n\n    dotnet tool install --global dotnet-ef\n    \nMake sure you have [MySQL 8.0 Server](https://dev.mysql.com/downloads/) installed on your system; or, use a [Docker image](https://hub.docker.com/_/mysql) instead of installing MySQL Server.  In a terminal, execute the following to spin up a Docker image of MySQL:\n\n    docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypassword -d mysql\n\n\n## Running the solution\n\nBefore the solution can be executed, Entity Framework migrations must be run to setup the database.\n\nConfigure connection string in project's appsettings.json, replacing the `username`, `password`, and `database` appropriately:\n\n```cs\n\"ConnectionStrings\": {\n  \"DefaultConnection\":\"server=localhost;userid=myusername;password=mypassword;database=mydatabase;\"\n},\n```\n\nExecute the migration using either Visual Studio Package Manager Console (from menu: Tools -\u003e NuGet Package Manager -\u003e Package Manager Console):\n\n    \u003e\u003e Update-Database\n\nOr, from the command line via DotNet CLI, execute the following command inside the project directory, **where the .csproj file is located**:\n\n    $ dotnet ef database update\n\nAfter running the migration, the database is created and web application is ready to be run.\n\nRun the solution via your IDE; or, execute the following command line\n\n    dotnet run\n\nThen, load via browser to either https or http endpoints:\n\n- https://localhost:5001\n- http://localhost:5000\n\n\n## Project Setup\n\nProject setup has already been completed in this repository, ready for use as a template for your next project.\n\nOtherwise, adapt the steps below to incorporate MySQL into your solution.\n\n### Install NuGet packages\n\nInstall the `MySql.EntityFrameworkCore` NuGet package in the ASP.NET web application.\n\nTo do this, you can use the `dotnet` command line by executing:\n\n    dotnet add package MySql.EntityFrameworkCore --version 5.0.3.1\n\nOr, edit the project's .csproj file and add the following line in the `PackageReference` item group:\n\n    \u003cPackageReference Include=\"MySql.EntityFrameworkCore\" Version=\"5.0.3.1\" /\u003e\n\n### Modify Startup.cs\n\nIn `Startup.cs` under `ConfigureServices()` method, replace the `UseSqlServer` / `UseSqlite` option with MySQL:\n\n```cs\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    // Add framework services.\n    services.AddDbContext\u003cApplicationDbContext\u003e(options =\u003e\n        options.UseMySQL(Configuration.GetConnectionString(\"DefaultConnection\")));\n```\n\n### Migration Issues with DbContext\n\nUpon upgrading MySQL Oracle Connector, Entity Framework migrations may fail with the errors:\n\n\u003e MySql.Data.MySqlClient.MySqlException (0x80004005): Specified key was too long; max key length is 3072 bytes\n\u003e \n\u003e MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max) NULL, PRIMARY KEY (`Id`))\n\u003e \n\u003e Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\n\u003e ```sql\n\u003e CREATE TABLE `AspNetRoles` (\n\u003e     `Id` TEXT NOT NULL,\n\u003e     `Name` TEXT NULL,\n\u003e     `NormalizedName` TEXT NULL,\n\u003e     `ConcurrencyStamp` TEXT NULL,\n\u003e     PRIMARY KEY (`Id`)\n\u003e );\n\u003e ```\n\u003e MySql.Data.MySqlClient.MySqlException (0x80004005): BLOB/TEXT column 'Id' used in key specification without a key length\n\u003e ```sql\n\u003e CREATE TABLE `AspNetRoles` (\n\u003e     `Id` nvarchar(450) NOT NULL,\n\u003e     `Name` nvarchar(256) NULL,\n\u003e     `NormalizedName` nvarchar(256) NULL,\n\u003e     `ConcurrencyStamp` nvarchar(max) NULL,\n\u003e     PRIMARY KEY (`Id`)\n\u003e );\n\u003e ```\n\u003e MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max) NULL,\n\n\nTo resolve this, add the following code within the ApplicationDbContext.cs `OnModelCreating()`.\n\n```cs\nusing Microsoft.AspNetCore.Identity;\nusing Microsoft.AspNetCore.Identity.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore;\n\npublic class ApplicationDbContext : IdentityDbContext\n{\n\n    // ...\n\n    protected override void OnModelCreating(ModelBuilder builder)\n    {\n        base.OnModelCreating(builder);\n\n        builder.Entity\u003cIdentityRole\u003e(entity =\u003e entity.Property(m =\u003e m.Id).HasMaxLength(450));\n        builder.Entity\u003cIdentityRole\u003e(entity =\u003e entity.Property(m =\u003e m.ConcurrencyStamp).HasColumnType(\"varchar(256)\"));\n\n        builder.Entity\u003cIdentityUserLogin\u003cstring\u003e\u003e(entity =\u003e\n        {\n            entity.Property(m =\u003e m.LoginProvider).HasMaxLength(127);\n            entity.Property(m =\u003e m.ProviderKey).HasMaxLength(127);\n        });\n\n        builder.Entity\u003cIdentityUserRole\u003cstring\u003e\u003e(entity =\u003e\n        {\n            entity.Property(m =\u003e m.UserId).HasMaxLength(127);\n            entity.Property(m =\u003e m.RoleId).HasMaxLength(127);\n        });\n\n        builder.Entity\u003cIdentityUserToken\u003cstring\u003e\u003e(entity =\u003e\n        {\n            entity.Property(m =\u003e m.UserId).HasMaxLength(127);\n            entity.Property(m =\u003e m.LoginProvider).HasMaxLength(127);\n            entity.Property(m =\u003e m.Name).HasMaxLength(127);\n        });\n    }\n```\n\nThen, generate a new migration using Visual Studio Package Manager Console (from menu: Tools -\u003e NuGet Package Manager -\u003e Package Manager Console):\n\n    \u003e\u003e Add-Migration\n\nOr, from the command line via DotNet CLI:\n\n    $ dotnet ef migrations add CreateIdentitySchema\n    \n\n## Troubleshooting\n\n### Create Entity Framework Migration Table in MySQL\n\nIf running `dotnet ef` fails initially, the `__efmigrationshistory` table may not exist.  Past versions of Entity Framework migration tools failed to create this table.  \n\nAssure you're running the lastest tools:\n\n    dotnet tool update --global dotnet-ef\n\nOtherwise, manually create the migrations history table in the MySQL database by executing the following SQL script.\n\n```sql\nuse mydatabase;\n\nCREATE TABLE `mydatabase`.`__EFMigrationsHistory` (\n  `MigrationId` text NOT NULL,\n  `ProductVersion` text NOT NULL,\n  PRIMARY KEY (`MigrationId`(255)));\n```\n\n### Deprecated MySQL NuGet Packages\n\nNote that `MySql.Data.EntityFrameworkCore` NuGet package is deprecated, and is now: `MySql.EntityFrameworkCore`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonsturges%2Fmysql-dotnet-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonsturges%2Fmysql-dotnet-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonsturges%2Fmysql-dotnet-core/lists"}