Skip to content

Commit 368f9d1

Browse files
committed
Add HasCharSet method for ComplexPropertyBuilder, ComplexTypePropertyBuilder
1 parent 6ca7cba commit 368f9d1

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/EFCore.MySql/Extensions/MySqlPropertyBuilderExtensions.cs

+58
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,42 @@ public static PropertyBuilder HasCharSet(
149149
return propertyBuilder;
150150
}
151151

152+
/// <summary>
153+
/// Configures the charset for the property's column.
154+
/// </summary>
155+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
156+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
157+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
158+
public static ComplexPropertyBuilder HasCharSet(
159+
[NotNull] this ComplexPropertyBuilder propertyBuilder,
160+
string charSet)
161+
{
162+
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
163+
164+
var property = propertyBuilder.Metadata;
165+
property.SetCharSet(charSet);
166+
167+
return propertyBuilder;
168+
}
169+
170+
/// <summary>
171+
/// Configures the charset for the property's column.
172+
/// </summary>
173+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
174+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
175+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
176+
public static ComplexTypePropertyBuilder HasCharSet(
177+
[NotNull] this ComplexTypePropertyBuilder propertyBuilder,
178+
string charSet)
179+
{
180+
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
181+
182+
var property = propertyBuilder.Metadata;
183+
property.SetCharSet(charSet);
184+
185+
return propertyBuilder;
186+
}
187+
152188
/// <summary>
153189
/// Configures the charset for the property's column.
154190
/// </summary>
@@ -160,6 +196,28 @@ public static PropertyBuilder<TProperty> HasCharSet<TProperty>(
160196
string charSet)
161197
=> (PropertyBuilder<TProperty>)HasCharSet((PropertyBuilder)propertyBuilder, charSet);
162198

199+
/// <summary>
200+
/// Configures the charset for the property's column.
201+
/// </summary>
202+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
203+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
204+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
205+
public static ComplexPropertyBuilder<TProperty> HasCharSet<TProperty>(
206+
[NotNull] this ComplexPropertyBuilder<TProperty> propertyBuilder,
207+
string charSet)
208+
=> (ComplexPropertyBuilder<TProperty>)HasCharSet((ComplexPropertyBuilder)propertyBuilder, charSet);
209+
210+
/// <summary>
211+
/// Configures the charset for the property's column.
212+
/// </summary>
213+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
214+
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
215+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
216+
public static ComplexTypePropertyBuilder<TProperty> HasCharSet<TProperty>(
217+
[NotNull] this ComplexTypePropertyBuilder<TProperty> propertyBuilder,
218+
string charSet)
219+
=> (ComplexTypePropertyBuilder<TProperty>)HasCharSet((ComplexTypePropertyBuilder)propertyBuilder, charSet);
220+
163221
/// <summary>
164222
/// Configures the charset for the property's column.
165223
/// </summary>

src/EFCore.MySql/Extensions/MySqlPropertyExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ internal static string GetMySqlLegacyCharSet([NotNull] this IReadOnlyProperty pr
548548
public static void SetCharSet([NotNull] this IMutableProperty property, string charSet)
549549
=> property.SetOrRemoveAnnotation(MySqlAnnotationNames.CharSet, charSet);
550550

551+
/// <summary>
552+
/// Sets the name of the charset in use by the column of the property.
553+
/// </summary>
554+
/// <param name="property">The property to set the columns charset for.</param>
555+
/// <param name="charSet">The name of the charset used for the column of the property.</param>
556+
public static void SetCharSet([NotNull] this IMutableComplexProperty property, string charSet)
557+
=> property.SetOrRemoveAnnotation(MySqlAnnotationNames.CharSet, charSet);
558+
551559
/// <summary>
552560
/// Sets the name of the charset in use by the column of the property.
553561
/// </summary>

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)