MySql

.NET Framework

Registering MySql Provider

MySQL has a .NET provider named MySql.Data. You need to first install it in MyProject.Web:

Install-Package MySql.Data -Project MyProject.Web

If you didn’t install this provider in GAC/machine.config before, or don’t want to install it there, you need to register it in web.config file (MySql.Data NuGet package already does this on install):

  1. <configuration>
  2. // ...
  3. <system.data>
  4. <DbProviderFactories>
  5. <remove invariant="MySql.Data.MySqlClient"/>
  6. <add name="MySQL Data Provider"
  7. invariant="MySql.Data.MySqlClient"
  8. description=".Net Framework Data Provider for MySQL"
  9. type="MySql.Data.MySqlClient.MySqlClientFactory,
  10. MySql.Data, Culture=neutral,
  11. PublicKeyToken=c5687fc88969c44d" />
  12. </DbProviderFactories>
  13. </system.data>
  14. // ...

Setting Connection Strings

Next step is to replace connection strings for databases you want to use with MySql:

Make sure you replace connection string parameters with values for your server

  1. <connectionStrings>
  2. <add name="Default" connectionString="
  3. Server=localhost; Port=3306; Database=Serene_Default_v1;
  4. Uid=root; Pwd=yourpass"
  5. providerName="MySql.Data.MySqlClient" />
  6. <add name="Northwind" connectionString="
  7. Server=localhost; Port=3306; Database=Serene_Northwind_v1;
  8. Uid=root; Pwd=yourpass"
  9. providerName="MySql.Data.MySqlClient" />
  10. </connectionStrings>

Provider name must be MySql.Data.MySqlClient for Serenity to auto-detect dialect. Read notes above to override default dialect.

MySql uses lowercase database (schema) and table names, but doesn’t have the case sensitivity problem we talked about Postgres.

Configuring Code Generator

Sergen doesn’t have reference to MySql provider, so if you want to use it to generate code, you must also register this provider with it.

Sergen.exe is an exe file, so you can’t add a NuGet reference to it. We need to register this provider in application config file.

It is also possible to register the provider in GAC/machine.config and skip this step completely.

Locate Sergen.exe, which is under a folder like packages/Serenity.CodeGenerator.1.8.6/tools and create a file named Sergen.exe.config next to it with contents below:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <system.data>
  4. <DbProviderFactories>
  5. <remove invariant="MySql.Data.MySqlClient"/>
  6. <add name="MySQL Data Provider"
  7. invariant="MySql.Data.MySqlClient"
  8. description=".Net Framework Data Provider for MySQL"
  9. type="MySql.Data.MySqlClient.MySqlClientFactory,
  10. MySql.Data, Culture=neutral,
  11. PublicKeyToken=c5687fc88969c44d" />
  12. </DbProviderFactories>
  13. </system.data>
  14. <appSettings>
  15. <add key="LoadProviderDLLs" value="MySql.Data.dll"/>
  16. </appSettings>
  17. </configuration>

Also copy MySql.Data.dll to same folder where Sergen.exe resides. Now Sergen will be able to generate code for your MySql tables.

.NET Core

Registering MySql Provider

MySQL has a .NET provider named MySql.Data. You need to first install it in MyProject.AspNetCore:

Open project.json and add package as follows:

  1. {
  2. "dependencies": {
  3. // ...
  4. "Serenity.FluentMigrator.Runner": "1.6.903",
  5. "MySql.Data": "7.0.6-IR31"
  6. },

Make sure you have Serenity.FluentMigrator.Runner 1.6.903+

Open Initialization/Startup.cs file, register this factory in Serenity:

  1. DbProviderFactories.RegisterFactory(
  2. "System.Data.SqlClient", SqlClientFactory.Instance);
  3. DbProviderFactories.RegisterFactory(
  4. "MySql.Data.MySqlClient",
  5. MySql.Data.MySqlClient.MySqlClientFactory.Instance);

Configuring Code Generator

As of writing dotnet-sergen doesn’t yet support any databases other than Sql Server.