Skip to content

Commit 5ba4f72

Browse files
committed
Use Microting for MySQL on EF Core; add quick toggles in project files
1 parent 8ed3a7c commit 5ba4f72

8 files changed

Lines changed: 39 additions & 37 deletions

File tree

Connectors/src/MySql/Controllers/HomeController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using System.Data.Common;
22
using System.Diagnostics;
33
using Microsoft.AspNetCore.Mvc;
4-
using MySqlConnector;
54
using Steeltoe.Connectors;
65
using Steeltoe.Connectors.MySql;
76
using Steeltoe.Samples.MySql.Models;
87

98
namespace Steeltoe.Samples.MySql.Controllers;
109

11-
public sealed class HomeController(ConnectorFactory<MySqlOptions, MySqlConnection> connector) : Controller
10+
public sealed class HomeController(ConnectorFactory<MySqlOptions, MySqlConnectionAlias> connector) : Controller
1211
{
13-
private readonly Connector<MySqlOptions, MySqlConnection> _connector = connector.Get();
12+
private readonly Connector<MySqlOptions, MySqlConnectionAlias> _connector = connector.Get();
1413

1514
public async Task<IActionResult> Index(CancellationToken cancellationToken)
1615
{
@@ -20,9 +19,9 @@ public async Task<IActionResult> Index(CancellationToken cancellationToken)
2019
ConnectionString = _connector.Options.ConnectionString
2120
};
2221

23-
await using MySqlConnection connection = _connector.GetConnection();
22+
await using MySqlConnectionAlias connection = _connector.GetConnection();
2423
await connection.OpenAsync(cancellationToken);
25-
var command = new MySqlCommand("SELECT * FROM TestData;", connection);
24+
var command = new MySqlCommandAlias("SELECT * FROM TestData;", connection);
2625
await using DbDataReader reader = await command.ExecuteReaderAsync(cancellationToken);
2726

2827
while (await reader.ReadAsync(cancellationToken))
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using MySqlConnector;
21
using Steeltoe.Connectors;
32
using Steeltoe.Connectors.MySql;
43

@@ -8,30 +7,30 @@ internal sealed class MySqlSeeder
87
{
98
public static async Task CreateSampleDataAsync(IServiceProvider serviceProvider)
109
{
11-
var connectorFactory = serviceProvider.GetRequiredService<ConnectorFactory<MySqlOptions, MySqlConnection>>();
12-
await using MySqlConnection connection = connectorFactory.Get().GetConnection();
10+
var connectorFactory = serviceProvider.GetRequiredService<ConnectorFactory<MySqlOptions, MySqlConnectionAlias>>();
11+
await using MySqlConnectionAlias connection = connectorFactory.Get().GetConnection();
1312

1413
await connection.OpenAsync();
1514

1615
await DropCreateTableAsync(connection);
1716
await InsertSampleDataAsync(connection);
1817
}
1918

20-
private static async Task DropCreateTableAsync(MySqlConnection connection)
19+
private static async Task DropCreateTableAsync(MySqlConnectionAlias connection)
2120
{
22-
var dropCommand = new MySqlCommand("DROP TABLE IF EXISTS TestData;", connection);
21+
var dropCommand = new MySqlCommandAlias("DROP TABLE IF EXISTS TestData;", connection);
2322
await dropCommand.ExecuteNonQueryAsync();
2423

25-
var createCommand = new MySqlCommand("CREATE TABLE IF NOT EXISTS TestData(Id INT PRIMARY KEY, MyText VARCHAR(255));", connection);
24+
var createCommand = new MySqlCommandAlias("CREATE TABLE IF NOT EXISTS TestData(Id INT PRIMARY KEY, MyText VARCHAR(255));", connection);
2625
await createCommand.ExecuteNonQueryAsync();
2726
}
2827

29-
private static async Task InsertSampleDataAsync(MySqlConnection connection)
28+
private static async Task InsertSampleDataAsync(MySqlConnectionAlias connection)
3029
{
31-
var insertCommand1 = new MySqlCommand("INSERT INTO TestData(Id, MyText) VALUES(1, 'Row1 Text');", connection);
30+
var insertCommand1 = new MySqlCommandAlias("INSERT INTO TestData(Id, MyText) VALUES(1, 'Row1 Text');", connection);
3231
await insertCommand1.ExecuteNonQueryAsync();
3332

34-
var insertCommand2 = new MySqlCommand("INSERT INTO TestData(Id, MyText) VALUES(2, 'Row2 Text');", connection);
33+
var insertCommand2 = new MySqlCommandAlias("INSERT INTO TestData(Id, MyText) VALUES(2, 'Row2 Text');", connection);
3534
await insertCommand2.ExecuteNonQueryAsync();
3635
}
3736
}

Connectors/src/MySql/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using MySqlConnector;
21
using Steeltoe.Configuration.CloudFoundry;
32
using Steeltoe.Connectors.MySql;
43
using Steeltoe.Management.Endpoint.Actuators.All;
@@ -21,7 +20,7 @@
2120
// Steeltoe: optionally change the MySQL connection string at runtime.
2221
builder.Services.Configure<MySqlOptions>(options =>
2322
{
24-
var connectionStringBuilder = new MySqlConnectionStringBuilder
23+
var connectionStringBuilder = new MySqlConnectionStringBuilderAlias
2524
{
2625
ConnectionString = options.ConnectionString,
2726
UseCompression = false

Connectors/src/MySql/Steeltoe.Samples.MySql.csproj

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<UseOraclePackage>false</UseOraclePackage>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<!-- Either one of these MySQL NuGet packages should work in this sample. -->
11-
<PackageReference Include="MySqlConnector" Version="2.5.*" />
12-
<!--
13-
<PackageReference Include="MySql.Data" Version="9.7.*" />
14-
-->
15-
11+
<PackageReference Include="MySql.Data" Condition="'$(UseOraclePackage)' == 'true'" Version="9.7.*" />
12+
<PackageReference Include="MySqlConnector" Condition="'$(UseOraclePackage)' != 'true'" Version="2.6.*" />
1613
<PackageReference Include="Steeltoe.Connectors" Version="$(SteeltoeVersion)" />
1714
<PackageReference Include="Steeltoe.Management.Endpoint" Version="$(SteeltoeVersion)" />
1815
</ItemGroup>
1916

17+
<ItemGroup>
18+
<Using Condition="'$(UseOraclePackage)' == 'true'" Alias="MySqlConnectionAlias" Include="MySql.Data.MySqlClient.MySqlConnection" />
19+
<Using Condition="'$(UseOraclePackage)' != 'true'" Alias="MySqlConnectionAlias" Include="MySqlConnector.MySqlConnection" />
20+
<Using Condition="'$(UseOraclePackage)' == 'true'" Alias="MySqlCommandAlias" Include="MySql.Data.MySqlClient.MySqlCommand" />
21+
<Using Condition="'$(UseOraclePackage)' != 'true'" Alias="MySqlCommandAlias" Include="MySqlConnector.MySqlCommand" />
22+
<Using Condition="'$(UseOraclePackage)' == 'true'" Alias="MySqlConnectionStringBuilderAlias" Include="MySql.Data.MySqlClient.MySqlConnectionStringBuilder" />
23+
<Using Condition="'$(UseOraclePackage)' != 'true'" Alias="MySqlConnectionStringBuilderAlias" Include="MySqlConnector.MySqlConnectionStringBuilder" />
24+
</ItemGroup>
2025
</Project>

Connectors/src/MySqlEFCore/Program.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using MySql.EntityFrameworkCore.Infrastructure;
21
using Steeltoe.Configuration.CloudFoundry;
32
using Steeltoe.Connectors.EntityFrameworkCore.MySql;
43
using Steeltoe.Connectors.MySql;
@@ -36,13 +35,13 @@
3635
// Steeltoe: Setup DbContext connection strings, optionally changing MySQL options at runtime.
3736
builder.Services.AddDbContext<AppDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, serviceOneName, null, untypedOptions =>
3837
{
39-
var mySqlOptions = (MySQLDbContextOptionsBuilder)untypedOptions;
38+
var mySqlOptions = (MySqlDbContextOptionsBuilderAlias)untypedOptions;
4039
mySqlOptions.CommandTimeout(20);
4140
}));
4241

4342
builder.Services.AddDbContext<OtherDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, serviceTwoName, null, untypedOptions =>
4443
{
45-
var mySqlOptions = (MySQLDbContextOptionsBuilder)untypedOptions;
44+
var mySqlOptions = (MySqlDbContextOptionsBuilderAlias)untypedOptions;
4645
mySqlOptions.CommandTimeout(25);
4746
}));
4847
}
@@ -54,7 +53,7 @@
5453
// Steeltoe: Setup DbContext connection string, optionally changing MySQL options at runtime.
5554
builder.Services.AddDbContext<AppDbContext>((serviceProvider, options) => options.UseMySql(serviceProvider, null, null, untypedOptions =>
5655
{
57-
var mySqlOptions = (MySQLDbContextOptionsBuilder)untypedOptions;
56+
var mySqlOptions = (MySqlDbContextOptionsBuilderAlias)untypedOptions;
5857
mySqlOptions.CommandTimeout(15);
5958
}));
6059
}

Connectors/src/MySqlEFCore/Steeltoe.Samples.MySqlEFCore.csproj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<UseOraclePackage>false</UseOraclePackage>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<!-- Either one of these MySQL EF Core NuGet packages should work in this sample. -->
11-
<PackageReference Include="MySql.EntityFrameworkCore" Version="10.0.*" />
12-
<!--
13-
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="$(EFCoreVersion)" />
14-
-->
15-
11+
<PackageReference Include="Microting.EntityFrameworkCore.MySql" Condition="'$(UseOraclePackage)' != 'true'" Version="$(EFCoreVersion)" />
12+
<PackageReference Include="MySql.EntityFrameworkCore" Condition="'$(UseOraclePackage)' == 'true'" Version="$(EFCoreVersion)" />
1613
<PackageReference Include="Steeltoe.Connectors.EntityFrameworkCore" Version="$(SteeltoeVersion)" />
1714
<PackageReference Include="Steeltoe.Management.Endpoint" Version="$(SteeltoeVersion)" />
1815
</ItemGroup>
1916

17+
<ItemGroup>
18+
<Using Condition="'$(UseOraclePackage)' == 'true'" Alias="MySqlDbContextOptionsBuilderAlias"
19+
Include="MySql.EntityFrameworkCore.Infrastructure.MySQLDbContextOptionsBuilder" />
20+
<Using Condition="'$(UseOraclePackage)' != 'true'" Alias="MySqlDbContextOptionsBuilderAlias"
21+
Include="Microsoft.EntityFrameworkCore.Infrastructure.MySqlDbContextOptionsBuilder" />
22+
</ItemGroup>
23+
2024
</Project>

Management/src/ActuatorApi/Directory.Build.props

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
</PropertyGroup>
55
<PropertyGroup>
66
<AspNetCoreVersion>10.0.*</AspNetCoreVersion>
7-
<EFCoreVersion>
8-
<!-- Temporary workaround: EF Core 10 package for Pomelo.EntityFrameworkCore.MySql is not available yet. -->
9-
9.0.*
10-
</EFCoreVersion>
7+
<EFCoreVersion>10.0.*</EFCoreVersion>
118
<OpenTelemetryVersion>1.15.*</OpenTelemetryVersion>
129
</PropertyGroup>
1310
</Project>

Management/src/ActuatorApi/Steeltoe.Samples.ActuatorApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
<PrivateAssets>all</PrivateAssets>
1616
</PackageReference>
17+
<PackageReference Include="Microting.EntityFrameworkCore.MySql" Version="$(EFCoreVersion)" />
1718
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="$(OpenTelemetryVersion)" />
1819
<PackageReference Include="OpenTelemetry.Extensions.Propagators" Version="$(OpenTelemetryVersion)" />
1920
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="$(OpenTelemetryVersion)" />
2021
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="$(OpenTelemetryVersion)" />
2122
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="$(OpenTelemetryVersion)" />
22-
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="$(EFCoreVersion)" />
2323
<PackageReference Include="Steeltoe.Connectors.EntityFrameworkCore" Version="$(SteeltoeVersion)" />
2424
<PackageReference Include="Steeltoe.Management.GitProperties.Build" Version="$(SteeltoeVersion)" />
2525
<PackageReference Include="Steeltoe.Management.Prometheus" Version="$(SteeltoeVersion)" />

0 commit comments

Comments
 (0)