From a302bac84fbf3600a4420d90a1b6c47497dbbc93 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Mon, 26 Oct 2020 18:51:59 -0700 Subject: [PATCH] Cleanup for UnicodeAttribute PR --- src/EFCore.Abstractions/UnicodeAttribute.cs | 16 +++++++------- .../Internal/CSharpEntityTypeGenerator.cs | 13 ++++++----- .../Conventions/UnicodeAttributeConvention.cs | 2 +- .../Internal/CSharpEntityTypeGeneratorTest.cs | 9 ++------ .../DataAnnotationTestBase.cs | 22 +++++++------------ 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/EFCore.Abstractions/UnicodeAttribute.cs b/src/EFCore.Abstractions/UnicodeAttribute.cs index 5b32d726de0..b676df97c21 100644 --- a/src/EFCore.Abstractions/UnicodeAttribute.cs +++ b/src/EFCore.Abstractions/UnicodeAttribute.cs @@ -11,18 +11,18 @@ namespace Microsoft.EntityFrameworkCore [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public sealed class UnicodeAttribute : Attribute { - /// - /// A value indicating whether the property can contain unicode characters or not. - /// - public bool IsUnicode { get; } - /// /// Initializes a new instance of the class. /// - /// A value indicating whether the property can contain unicode characters or not. - public UnicodeAttribute(bool isUnicode = true) + /// A value indicating whether the property can contain unicode characters or not. + public UnicodeAttribute(bool unicode = true) { - IsUnicode = isUnicode; + IsUnicode = unicode; } + + /// + /// A value indicating whether the property can contain unicode characters or not. + /// + public bool IsUnicode { get; } } } diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs index f1e28861471..8f70fa8a790 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs @@ -373,18 +373,19 @@ private void GenerateMaxLengthAttribute(IProperty property) private void GenerateUnicodeAttribute(IProperty property) { - CoreTypeMapping typeMapping = property.FindTypeMapping(); - if (property.ClrType != typeof(string) && typeMapping.Converter?.ProviderClrType != typeof(string)) + if (property.ClrType != typeof(string)) { return; } - var isUnicode = property.IsUnicode(); - - if (isUnicode.HasValue && !isUnicode.Value) + var unicode = property.IsUnicode(); + if (unicode.HasValue) { var unicodeAttribute = new AttributeWriter(nameof(UnicodeAttribute)); - unicodeAttribute.AddParameter(_code.Literal(false)); + if (!unicode.Value) + { + unicodeAttribute.AddParameter(_code.Literal(false)); + } _sb.AppendLine(unicodeAttribute.ToString()); } } diff --git a/src/EFCore/Metadata/Conventions/UnicodeAttributeConvention.cs b/src/EFCore/Metadata/Conventions/UnicodeAttributeConvention.cs index c62cc824ba2..80c34b9d849 100644 --- a/src/EFCore/Metadata/Conventions/UnicodeAttributeConvention.cs +++ b/src/EFCore/Metadata/Conventions/UnicodeAttributeConvention.cs @@ -35,7 +35,7 @@ protected override void ProcessPropertyAdded( MemberInfo clrMember, IConventionContext context) { - propertyBuilder.IsUnicode(unicode: attribute.IsUnicode, fromDataAnnotation: true); + propertyBuilder.IsUnicode(attribute.IsUnicode, fromDataAnnotation: true); } } } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs index 35d25e4c72b..99ea4f5f595 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Linq; using Microsoft.EntityFrameworkCore.Internal; using Xunit; @@ -859,8 +860,6 @@ public void UnicodeAttribute_is_generated_for_property() x.Property("A").HasMaxLength(34).IsUnicode(); x.Property("B").HasMaxLength(34).IsUnicode(false); x.Property("C").HasMaxLength(34); - x.Property("D").HasConversion(); - x.Property("E").HasConversion().IsUnicode(false); }), new ModelCodeGenerationOptions { UseDataAnnotations = true }, code => @@ -881,15 +880,13 @@ public partial class Entity [Key] public int Id { get; set; } [StringLength(34)] + [Unicode] public string A { get; set; } [StringLength(34)] [Unicode(false)] public string B { get; set; } [StringLength(34)] public string C { get; set; } - public DateTimeOffset D { get; set; } - [Unicode(false)] - public DateTimeOffset E { get; set; } } } ", @@ -901,8 +898,6 @@ public partial class Entity Assert.True(entitType.GetProperty("A").IsUnicode()); Assert.False(entitType.GetProperty("B").IsUnicode()); Assert.Null(entitType.GetProperty("C").IsUnicode()); - Assert.Null(entitType.GetProperty("D").IsUnicode()); - Assert.False(entitType.GetProperty("E").IsUnicode()); }); } diff --git a/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs b/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs index d062bd71ac1..2bba7f319ab 100644 --- a/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs +++ b/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs @@ -2379,29 +2379,22 @@ public virtual void TimestampAttribute_throws_if_value_in_database_changed() } [ConditionalFact] - public virtual void Unicode_annotation_is_enabled() + public virtual void UnicodeAttribute_sets_unicode_for_properties_and_fields() { var modelBuilder = CreateModelBuilder(); - modelBuilder.Entity(); + modelBuilder.Entity(b => + { + b.Property(e => e.PersonMiddleName); + b.Property(e => e.PersonAddress); + }); Validate(modelBuilder); Assert.True(GetProperty(modelBuilder, "PersonFirstName").IsUnicode()); Assert.False(GetProperty(modelBuilder, "PersonLastName").IsUnicode()); - } - - [ConditionalFact] - public virtual void Unicode_is_configured_explicitly_for_fields() - { - var modelBuilder = CreateModelBuilder(); - - modelBuilder.Entity().Property(n => n.PersonMiddleName); - modelBuilder.Entity().Property(n => n.PersonAddress); - Validate(modelBuilder); - - Assert.Null(GetProperty(modelBuilder, "PersonMiddleName").IsUnicode()); + Assert.True(GetProperty(modelBuilder, "PersonMiddleName").IsUnicode()); Assert.False(GetProperty(modelBuilder, "PersonAddress").IsUnicode()); } @@ -2415,6 +2408,7 @@ protected class UnicodeAnnotationClass [Unicode(false)] public string PersonLastName { get; set; } + [Unicode] public string PersonMiddleName; [Unicode(false)]