Skip to content

Add HasCharSet method for ComplexPropertyBuilder #1984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/EFCore.MySql/Design/Internal/MySqlAnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;

Expand Down Expand Up @@ -80,6 +81,12 @@ private static readonly MethodInfo _propertyHasCharSetMethodInfo
typeof(PropertyBuilder),
typeof(string));

private static readonly MethodInfo _complexTypePropertyHasCharSetMethodInfo
= typeof(MySqlComplexTypePropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(MySqlComplexTypePropertyBuilderExtensions.HasCharSet),
typeof(ComplexTypePropertyBuilder),
typeof(string));

public MySqlAnnotationCodeGenerator([JetBrains.Annotations.NotNull] AnnotationCodeGeneratorDependencies dependencies)
: base(dependencies)
{
Expand Down Expand Up @@ -262,6 +269,13 @@ protected override MethodCallCodeFragment GenerateFluentApi(IProperty property,
switch (annotation.Name)
{
case MySqlAnnotationNames.CharSet when annotation.Value is string { Length: > 0 } charSet:
if (property.DeclaringType is IComplexType)
{
return new MethodCallCodeFragment(
_complexTypePropertyHasCharSetMethodInfo,
charSet);
}

return new MethodCallCodeFragment(
_propertyHasCharSetMethodInfo,
charSet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Pomelo Foundation. All rights reserved.
// Licensed under the MIT. See LICENSE in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// MySQL specific extension methods for <see cref="ComplexTypePropertyBuilder" />.
/// </summary>
public static class MySqlComplexTypePropertyBuilderExtensions
{
/// <summary>
/// Configures the charset for the property's column.
/// </summary>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ComplexTypePropertyBuilder HasCharSet(
[NotNull] this ComplexTypePropertyBuilder propertyBuilder,
string charSet)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));

var property = propertyBuilder.Metadata;
property.SetCharSet(charSet);

return propertyBuilder;
}

/// <summary>
/// Configures the charset for the property's column.
/// </summary>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="charSet">The name of the charset to configure for the property's column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ComplexTypePropertyBuilder<TProperty> HasCharSet<TProperty>(
[NotNull] this ComplexTypePropertyBuilder<TProperty> propertyBuilder,
string charSet)
=> (ComplexTypePropertyBuilder<TProperty>)HasCharSet((ComplexTypePropertyBuilder)propertyBuilder, charSet);
}
}
11 changes: 10 additions & 1 deletion test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -1229,17 +1230,24 @@ await Test(
e.Property<string>("Name");
e.Property<string>("Brand")
.HasCharSet(NonDefaultCharSet);

e.ComplexProperty<Dictionary<string, object>>("ComplexProperty")
.Property<string>("Brand")
.HasCharSet(NonDefaultCharSet);
}),
result =>
{
var table = Assert.Single(result.Tables);
var nameColumn = Assert.Single(table.Columns.Where(c => c.Name == "Name"));
var brandColumn = Assert.Single(table.Columns.Where(c => c.Name == "Brand"));
var complexBrandColumn = Assert.Single(table.Columns.Where(c => c.Name == "ComplexProperty_Brand"));

Assert.Null(nameColumn[MySqlAnnotationNames.CharSet]);
Assert.Null(nameColumn.Collation);
Assert.Equal(NonDefaultCharSet, brandColumn[MySqlAnnotationNames.CharSet]);
Assert.NotEqual(DefaultCollation, brandColumn.Collation);
Assert.Equal(NonDefaultCharSet, complexBrandColumn[MySqlAnnotationNames.CharSet]);
Assert.NotEqual(DefaultCollation, complexBrandColumn.Collation);
});

AssertSql(
Expand All @@ -1249,6 +1257,7 @@ await Test(
`IceCreamId` int NOT NULL AUTO_INCREMENT,
`Brand` longtext CHARACTER SET {NonDefaultCharSet} NULL,
`Name` longtext COLLATE {DefaultCollation} NULL,
`ComplexProperty_Brand` longtext CHARACTER SET {NonDefaultCharSet} NULL,
CONSTRAINT `PK_IceCream` PRIMARY KEY (`IceCreamId`)
) COLLATE={DefaultCollation};");
}
Expand Down