Skip to content

Commit 24a3b5b

Browse files
authored
Add HasCharSet method for ComplexPropertyBuilder (#1984)
* Add MySqlComplexTypePropertyBuilderExtensions * Add ComplexProperty.Property.HasCharSet * Update GenerateFluentApi for ComplexProperty.Property.Charset * Drop region
1 parent 0bf88c5 commit 24a3b5b

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/EFCore.MySql/Design/Internal/MySqlAnnotationCodeGenerator.cs

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.EntityFrameworkCore.Infrastructure;
1313
using Microsoft.EntityFrameworkCore.Metadata;
1414
using Microsoft.EntityFrameworkCore.Metadata.Builders;
15+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
1516
using Microsoft.EntityFrameworkCore.Utilities;
1617
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;
1718

@@ -80,6 +81,12 @@ private static readonly MethodInfo _propertyHasCharSetMethodInfo
8081
typeof(PropertyBuilder),
8182
typeof(string));
8283

84+
private static readonly MethodInfo _complexTypePropertyHasCharSetMethodInfo
85+
= typeof(MySqlComplexTypePropertyBuilderExtensions).GetRequiredRuntimeMethod(
86+
nameof(MySqlComplexTypePropertyBuilderExtensions.HasCharSet),
87+
typeof(ComplexTypePropertyBuilder),
88+
typeof(string));
89+
8390
public MySqlAnnotationCodeGenerator([JetBrains.Annotations.NotNull] AnnotationCodeGeneratorDependencies dependencies)
8491
: base(dependencies)
8592
{
@@ -262,6 +269,13 @@ protected override MethodCallCodeFragment GenerateFluentApi(IProperty property,
262269
switch (annotation.Name)
263270
{
264271
case MySqlAnnotationNames.CharSet when annotation.Value is string { Length: > 0 } charSet:
272+
if (property.DeclaringType is IComplexType)
273+
{
274+
return new MethodCallCodeFragment(
275+
_complexTypePropertyHasCharSetMethodInfo,
276+
charSet);
277+
}
278+
265279
return new MethodCallCodeFragment(
266280
_propertyHasCharSetMethodInfo,
267281
charSet);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Pomelo Foundation. All rights reserved.
2+
// Licensed under the MIT. See LICENSE in the project root for license information.
3+
4+
using JetBrains.Annotations;
5+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
6+
using Microsoft.EntityFrameworkCore.Utilities;
7+
8+
// ReSharper disable once CheckNamespace
9+
namespace Microsoft.EntityFrameworkCore
10+
{
11+
/// <summary>
12+
/// MySQL specific extension methods for <see cref="ComplexTypePropertyBuilder" />.
13+
/// </summary>
14+
public static class MySqlComplexTypePropertyBuilderExtensions
15+
{
16+
/// <summary>
17+
/// Configures the charset for the property's column.
18+
/// </summary>
19+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
20+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
21+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
22+
public static ComplexTypePropertyBuilder HasCharSet(
23+
[NotNull] this ComplexTypePropertyBuilder propertyBuilder,
24+
string charSet)
25+
{
26+
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
27+
28+
var property = propertyBuilder.Metadata;
29+
property.SetCharSet(charSet);
30+
31+
return propertyBuilder;
32+
}
33+
34+
/// <summary>
35+
/// Configures the charset for the property's column.
36+
/// </summary>
37+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
38+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
39+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
40+
public static ComplexTypePropertyBuilder<TProperty> HasCharSet<TProperty>(
41+
[NotNull] this ComplexTypePropertyBuilder<TProperty> propertyBuilder,
42+
string charSet)
43+
=> (ComplexTypePropertyBuilder<TProperty>)HasCharSet((ComplexTypePropertyBuilder)propertyBuilder, charSet);
44+
}
45+
}

test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Microsoft.EntityFrameworkCore;
@@ -1229,17 +1230,24 @@ await Test(
12291230
e.Property<string>("Name");
12301231
e.Property<string>("Brand")
12311232
.HasCharSet(NonDefaultCharSet);
1233+
1234+
e.ComplexProperty<Dictionary<string, object>>("ComplexProperty")
1235+
.Property<string>("Brand")
1236+
.HasCharSet(NonDefaultCharSet);
12321237
}),
12331238
result =>
12341239
{
12351240
var table = Assert.Single(result.Tables);
12361241
var nameColumn = Assert.Single(table.Columns.Where(c => c.Name == "Name"));
12371242
var brandColumn = Assert.Single(table.Columns.Where(c => c.Name == "Brand"));
1243+
var complexBrandColumn = Assert.Single(table.Columns.Where(c => c.Name == "ComplexProperty_Brand"));
12381244

12391245
Assert.Null(nameColumn[MySqlAnnotationNames.CharSet]);
12401246
Assert.Null(nameColumn.Collation);
12411247
Assert.Equal(NonDefaultCharSet, brandColumn[MySqlAnnotationNames.CharSet]);
12421248
Assert.NotEqual(DefaultCollation, brandColumn.Collation);
1249+
Assert.Equal(NonDefaultCharSet, complexBrandColumn[MySqlAnnotationNames.CharSet]);
1250+
Assert.NotEqual(DefaultCollation, complexBrandColumn.Collation);
12431251
});
12441252

12451253
AssertSql(
@@ -1249,6 +1257,7 @@ await Test(
12491257
`IceCreamId` int NOT NULL AUTO_INCREMENT,
12501258
`Brand` longtext CHARACTER SET {NonDefaultCharSet} NULL,
12511259
`Name` longtext COLLATE {DefaultCollation} NULL,
1260+
`ComplexProperty_Brand` longtext CHARACTER SET {NonDefaultCharSet} NULL,
12521261
CONSTRAINT `PK_IceCream` PRIMARY KEY (`IceCreamId`)
12531262
) COLLATE={DefaultCollation};");
12541263
}

0 commit comments

Comments
 (0)