From 937a163d0e7b2decd5e79318a194acb86038af7c Mon Sep 17 00:00:00 2001 From: hello Date: Sun, 24 Nov 2024 18:58:58 +0800 Subject: [PATCH] Add unit test files. --- EfCore.NamingConverter.Sample/MyDbContext.cs | 6 +- EfCore.NamingConverter.Sample/Program.cs | 2 +- .../CamelCaseNamingPolicyTests.cs | 93 +++++++++++++ .../ColumnNameConventionTests.cs | 17 +++ .../ConvertNamingPolicyTests.cs | 51 +++++++ .../EfCore.NamingConverter.Tests.csproj | 1 + .../KebabCaseLowerNamingPolicyTests.cs | 78 +++++++++++ .../KebabCaseUpperNamingPolicyTests.cs | 75 +++++++++++ .../SeparatorNamingPolicyTests.cs | 124 ++++++++++++++++++ .../SnakeCaseLowerNamingPolicyTests.cs | 105 +++++++++++++++ .../SnakeCaseUpperNamingPolicyTests.cs | 104 +++++++++++++++ .../TableNameConventionTests.cs | 18 +++ .../EfCore.NamingConverter.csproj | 9 +- EfCore.NamingConverter/Extensions.cs | 4 +- .../SeparatorNamingPolicy.cs | 6 +- README.md | 13 +- README.zh-CN.md | 39 ------ 17 files changed, 688 insertions(+), 57 deletions(-) create mode 100644 EfCore.NamingConverter.Tests/CamelCaseNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/ColumnNameConventionTests.cs create mode 100644 EfCore.NamingConverter.Tests/ConvertNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/KebabCaseLowerNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/KebabCaseUpperNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/SeparatorNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/SnakeCaseLowerNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/SnakeCaseUpperNamingPolicyTests.cs create mode 100644 EfCore.NamingConverter.Tests/TableNameConventionTests.cs delete mode 100644 README.zh-CN.md diff --git a/EfCore.NamingConverter.Sample/MyDbContext.cs b/EfCore.NamingConverter.Sample/MyDbContext.cs index d75361f..f5fa435 100644 --- a/EfCore.NamingConverter.Sample/MyDbContext.cs +++ b/EfCore.NamingConverter.Sample/MyDbContext.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; -namespace EfCore.NamingConverter +namespace EfCore.NamingConverter.Sample { public class MyDbContext : DbContext { @@ -12,12 +12,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); - optionsBuilder.UseNpgsql("Host=localhost;Database=testdb;Username=postgres;Password=postgres"); + optionsBuilder.UseNpgsql("Host=localhost;Database=mydb;Username=postgres;Password=postgres"); } protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) { - configurationBuilder.AddNamingConventions(NamingPolicy.CamelCase); + configurationBuilder.AddNamingConventions(NamingPolicy.SnakeCaseLower); } } } diff --git a/EfCore.NamingConverter.Sample/Program.cs b/EfCore.NamingConverter.Sample/Program.cs index 1ee791d..e77b3be 100644 --- a/EfCore.NamingConverter.Sample/Program.cs +++ b/EfCore.NamingConverter.Sample/Program.cs @@ -1,4 +1,4 @@ -using EfCore.NamingConverter; +using EfCore.NamingConverter.Sample; MyDbContext context = new(); diff --git a/EfCore.NamingConverter.Tests/CamelCaseNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/CamelCaseNamingPolicyTests.cs new file mode 100644 index 0000000..e5fd518 --- /dev/null +++ b/EfCore.NamingConverter.Tests/CamelCaseNamingPolicyTests.cs @@ -0,0 +1,93 @@ +namespace EfCore.NamingConverter.Tests +{ + public class CamelCaseNamingPolicyTests + { + [Fact] + public void ConvertName_ShouldConvertToCamelCase() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + var input = "TestName"; + var expected = "testName"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleNullInput() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + + // Act + var result = policy.ConvertName(null); + + // Assert + Assert.Null(result); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + var input = "Word"; + var expected = "word"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + var input = "MultipleWordsInString"; + var expected = "multipleWordsInString"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldNotChangeAlreadyCamelCase() + { + // Arrange + var policy = new CamelCaseNamingPolicy(); + var input = "alreadyCamelCase"; + var expected = "alreadyCamelCase"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} diff --git a/EfCore.NamingConverter.Tests/ColumnNameConventionTests.cs b/EfCore.NamingConverter.Tests/ColumnNameConventionTests.cs new file mode 100644 index 0000000..85a7a66 --- /dev/null +++ b/EfCore.NamingConverter.Tests/ColumnNameConventionTests.cs @@ -0,0 +1,17 @@ +namespace EfCore.NamingConverter.Tests +{ + public class ColumnNameConventionTests + { + [Theory] + [InlineData(NamingPolicy.CamelCase, "TestName", "testName")] + [InlineData(NamingPolicy.SnakeCaseLower, "TestName", "test_name")] + [InlineData(NamingPolicy.SnakeCaseUpper, "TestName", "TEST_NAME")] + [InlineData(NamingPolicy.KebabCaseLower, "TestName", "test-name")] + [InlineData(NamingPolicy.KebabCaseUpper, "TestName", "TEST-NAME")] + public void ProcessPropertyAdded_ShouldConvertColumnName(NamingPolicy namingPolicy, string originalName, string expectedName) + { + // TODO: The test is not complete. You need to create a mock for IConventionPropertyBuilder and IConventionContext + } + } +} + diff --git a/EfCore.NamingConverter.Tests/ConvertNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/ConvertNamingPolicyTests.cs new file mode 100644 index 0000000..d228345 --- /dev/null +++ b/EfCore.NamingConverter.Tests/ConvertNamingPolicyTests.cs @@ -0,0 +1,51 @@ +namespace EfCore.NamingConverter.Tests +{ + public class ConvertNamingPolicyTests + { + [Theory] + [InlineData(NamingPolicy.Unspecified, typeof(NoneCaseNamingPolicy))] + [InlineData(NamingPolicy.CamelCase, typeof(CamelCaseNamingPolicy))] + [InlineData(NamingPolicy.SnakeCaseLower, typeof(SnakeCaseLowerNamingPolicy))] + [InlineData(NamingPolicy.SnakeCaseUpper, typeof(SnakeCaseUpperNamingPolicy))] + [InlineData(NamingPolicy.KebabCaseLower, typeof(KebabCaseLowerNamingPolicy))] + [InlineData(NamingPolicy.KebabCaseUpper, typeof(KebabCaseUpperNamingPolicy))] + public void From_ShouldReturnCorrectPolicy(NamingPolicy namingPolicy, Type expectedType) + { + // Act + var result = ConvertNamingPolicy.From(namingPolicy); + + // Assert + Assert.IsType(expectedType, result); + } + + [Fact] + public void From_ShouldThrowArgumentOutOfRangeExceptionForInvalidPolicy() + { + // Arrange + var invalidPolicy = (NamingPolicy)999; + + // Act & Assert + Assert.Throws(() => ConvertNamingPolicy.From(invalidPolicy)); + } + + [Theory] + [InlineData("TestName", "testName", NamingPolicy.CamelCase)] + [InlineData("TestName", "test_name", NamingPolicy.SnakeCaseLower)] + [InlineData("TestName", "TEST_NAME", NamingPolicy.SnakeCaseUpper)] + [InlineData("TestName", "test-name", NamingPolicy.KebabCaseLower)] + [InlineData("TestName", "TEST-NAME", NamingPolicy.KebabCaseUpper)] + [InlineData("TestName", "TestName", NamingPolicy.Unspecified)] + public void ConvertName_ShouldConvertNameAccordingToPolicy(string input, string expected, NamingPolicy namingPolicy) + { + // Arrange + var policy = ConvertNamingPolicy.From(namingPolicy); + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} + diff --git a/EfCore.NamingConverter.Tests/EfCore.NamingConverter.Tests.csproj b/EfCore.NamingConverter.Tests/EfCore.NamingConverter.Tests.csproj index 9361346..a155c5f 100644 --- a/EfCore.NamingConverter.Tests/EfCore.NamingConverter.Tests.csproj +++ b/EfCore.NamingConverter.Tests/EfCore.NamingConverter.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/EfCore.NamingConverter.Tests/KebabCaseLowerNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/KebabCaseLowerNamingPolicyTests.cs new file mode 100644 index 0000000..3776cb6 --- /dev/null +++ b/EfCore.NamingConverter.Tests/KebabCaseLowerNamingPolicyTests.cs @@ -0,0 +1,78 @@ +namespace EfCore.NamingConverter.Tests +{ + public class KebabCaseLowerNamingPolicyTests + { + [Fact] + public void ConvertName_ShouldConvertToKebabCaseLower() + { + // Arrange + var policy = new KebabCaseLowerNamingPolicy(); + var input = "TestName"; + var expected = "test-name"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldThrowOnNullInput() + { + // Arrange + var policy = new KebabCaseLowerNamingPolicy(); + + // Act + void action() => policy.ConvertName(null); + + // Assert + Assert.Throws(action); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new KebabCaseLowerNamingPolicy(); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new KebabCaseLowerNamingPolicy(); + var input = "Word"; + var expected = "word"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new KebabCaseLowerNamingPolicy(); + var input = "MultipleWordsInString"; + var expected = "multiple-words-in-string"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} diff --git a/EfCore.NamingConverter.Tests/KebabCaseUpperNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/KebabCaseUpperNamingPolicyTests.cs new file mode 100644 index 0000000..5437304 --- /dev/null +++ b/EfCore.NamingConverter.Tests/KebabCaseUpperNamingPolicyTests.cs @@ -0,0 +1,75 @@ +namespace EfCore.NamingConverter.Tests +{ + public class KebabCaseUpperNamingPolicyTests + { + [Fact] + public void ConvertName_ShouldConvertToKebabCaseUpper() + { + // Arrange + var policy = new KebabCaseUpperNamingPolicy(); + var input = "TestName"; + var expected = "TEST-NAME"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldThrowArgumentNullException() + { + // Arrange + var policy = new KebabCaseUpperNamingPolicy(); + + // Act & Assert + Assert.Throws(() => policy.ConvertName(null)); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new KebabCaseUpperNamingPolicy(); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new KebabCaseUpperNamingPolicy(); + var input = "Word"; + var expected = "WORD"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new KebabCaseUpperNamingPolicy(); + var input = "MultipleWordsInString"; + var expected = "MULTIPLE-WORDS-IN-STRING"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} \ No newline at end of file diff --git a/EfCore.NamingConverter.Tests/SeparatorNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/SeparatorNamingPolicyTests.cs new file mode 100644 index 0000000..4c5a704 --- /dev/null +++ b/EfCore.NamingConverter.Tests/SeparatorNamingPolicyTests.cs @@ -0,0 +1,124 @@ +namespace EfCore.NamingConverter.Tests +{ + public class SeparatorNamingPolicyTests + { + private class TestSeparatorNamingPolicy(bool lowercase, char separator) : SeparatorNamingPolicy(lowercase, separator) + { + } + + [Fact] + public void ConvertName_ShouldConvertToLowercaseWithSeparator() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = "TestName"; + var expected = "test-name"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldConvertToUppercaseWithSeparator() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: false, separator: '-'); + var input = "TestName"; + var expected = "TEST-NAME"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleNullInput() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + + // Act & Assert + Assert.Throws(() => policy.ConvertName(null)); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = "Word"; + var expected = "word"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = "MultipleWordsInString"; + var expected = "multiple-words-in-string"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSpaces() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = "Multiple Words In String"; + var expected = "multiple-words-in-string"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleNonAlphanumericCharacters() + { + // Arrange + var policy = new TestSeparatorNamingPolicy(lowercase: true, separator: '-'); + var input = "Test@Name#With$Special%Characters"; + var expected = "test@name#with$special%characters"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} \ No newline at end of file diff --git a/EfCore.NamingConverter.Tests/SnakeCaseLowerNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/SnakeCaseLowerNamingPolicyTests.cs new file mode 100644 index 0000000..43c2d01 --- /dev/null +++ b/EfCore.NamingConverter.Tests/SnakeCaseLowerNamingPolicyTests.cs @@ -0,0 +1,105 @@ +namespace EfCore.NamingConverter.Tests +{ + public class SnakeCaseLowerNamingPolicyTests + { + [Fact] + public void ConvertName_ShouldConvertToSnakeCaseLower() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = "TestName"; + var expected = "test_name"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldThrowArgumentNullExceptionOnNullInput() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + + // Act & Assert + Assert.Throws(() => policy.ConvertName(null)); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = "Word"; + var expected = "word"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = "MultipleWordsInString"; + var expected = "multiple_words_in_string"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSpaces() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = "Multiple Words In String"; + var expected = "multiple_words_in_string"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleNonAlphanumericCharacters() + { + // Arrange + var policy = new SnakeCaseLowerNamingPolicy(); + var input = "Test@Name#With$Special%Characters"; + var expected = "test@name#with$special%characters"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} diff --git a/EfCore.NamingConverter.Tests/SnakeCaseUpperNamingPolicyTests.cs b/EfCore.NamingConverter.Tests/SnakeCaseUpperNamingPolicyTests.cs new file mode 100644 index 0000000..316b8eb --- /dev/null +++ b/EfCore.NamingConverter.Tests/SnakeCaseUpperNamingPolicyTests.cs @@ -0,0 +1,104 @@ +namespace EfCore.NamingConverter.Tests +{ + public class SnakeCaseUpperNamingPolicyTests + { + [Fact] + public void ConvertName_ShouldConvertToSnakeCaseUpper() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = "TestName"; + var expected = "TEST_NAME"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldThrowArgumentNullException() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + // Act & Assert + Assert.Throws(() => policy.ConvertName(null)); + } + + [Fact] + public void ConvertName_ShouldHandleEmptyString() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = string.Empty; + var expected = string.Empty; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSingleWord() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = "Word"; + var expected = "WORD"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleMultipleWords() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = "MultipleWordsInString"; + var expected = "MULTIPLE_WORDS_IN_STRING"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleSpaces() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = "Multiple Words In String"; + var expected = "MULTIPLE_WORDS_IN_STRING"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void ConvertName_ShouldHandleNonAlphanumericCharacters() + { + // Arrange + var policy = new SnakeCaseUpperNamingPolicy(); + var input = "Test@Name#With$Special%Characters"; + var expected = "TEST@NAME#WITH$SPECIAL%CHARACTERS"; + + // Act + var result = policy.ConvertName(input); + + // Assert + Assert.Equal(expected, result); + } + } +} \ No newline at end of file diff --git a/EfCore.NamingConverter.Tests/TableNameConventionTests.cs b/EfCore.NamingConverter.Tests/TableNameConventionTests.cs new file mode 100644 index 0000000..e5e4504 --- /dev/null +++ b/EfCore.NamingConverter.Tests/TableNameConventionTests.cs @@ -0,0 +1,18 @@ +namespace EfCore.NamingConverter.Tests +{ + public class TableNameConventionTests + { + [Theory] + [InlineData(NamingPolicy.CamelCase, "TestTable", "testTable")] + [InlineData(NamingPolicy.SnakeCaseLower, "TestTable", "test_table")] + [InlineData(NamingPolicy.SnakeCaseUpper, "TestTable", "TEST_TABLE")] + [InlineData(NamingPolicy.KebabCaseLower, "TestTable", "test-table")] + [InlineData(NamingPolicy.KebabCaseUpper, "TestTable", "TEST-TABLE")] + [InlineData(NamingPolicy.Unspecified, "TestTable", "TestTable")] + public void ProcessEntityTypeAdded_ShouldConvertTableName(NamingPolicy namingPolicy, string originalName, string expectedName) + { + // TODO: This test is not complete. You need to create a mock for IConventionEntityTypeBuilder and call the ProcessEntityTypeAdded method. + } + } +} + diff --git a/EfCore.NamingConverter/EfCore.NamingConverter.csproj b/EfCore.NamingConverter/EfCore.NamingConverter.csproj index 2d49470..1df51dc 100644 --- a/EfCore.NamingConverter/EfCore.NamingConverter.csproj +++ b/EfCore.NamingConverter/EfCore.NamingConverter.csproj @@ -6,9 +6,9 @@ enable bit365 Entity Framework Core Naming Converter - https://github.com/bit365/EfCore.NamingConverter - README.md - 1.0.3 + https://github.com/bit365/efcore-naming-converter + ../README.md + 1.0.5 @@ -17,5 +17,8 @@ + + + \ No newline at end of file diff --git a/EfCore.NamingConverter/Extensions.cs b/EfCore.NamingConverter/Extensions.cs index 8c98b06..f9b3b77 100644 --- a/EfCore.NamingConverter/Extensions.cs +++ b/EfCore.NamingConverter/Extensions.cs @@ -1,6 +1,6 @@ -using EfCore.NamingConverter; +using Microsoft.EntityFrameworkCore; -namespace Microsoft.EntityFrameworkCore +namespace EfCore.NamingConverter { public static class Extensions { diff --git a/EfCore.NamingConverter/SeparatorNamingPolicy.cs b/EfCore.NamingConverter/SeparatorNamingPolicy.cs index 1449632..98bfcee 100644 --- a/EfCore.NamingConverter/SeparatorNamingPolicy.cs +++ b/EfCore.NamingConverter/SeparatorNamingPolicy.cs @@ -127,11 +127,11 @@ private static string ConvertNameCore(char separator, bool lowercase, ReadOnlySp } } - string result = destination.Slice(0, charsWritten).ToString(); + string result = destination[..charsWritten].ToString(); if (rentedBuffer is not null) { - destination.Slice(0, charsWritten).Clear(); + destination[..charsWritten].Clear(); ArrayPool.Shared.Return(rentedBuffer); } @@ -156,7 +156,7 @@ void ExpandBuffer(ref Span destination) if (rentedBuffer is not null) { - destination.Slice(0, charsWritten).Clear(); + destination[..charsWritten].Clear(); ArrayPool.Shared.Return(rentedBuffer); } diff --git a/README.md b/README.md index b37b0dc..e584db9 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ dotnet add package EfCore.NamingConverter Using the naming converter in the DbContext class is the default configuration method by Microsoft. You only need to add the naming converter in the ConfigureConventions method. In the next version, we are considering providing DbContextOptions configuration options for easier configuration. ```csharp +using EfCore.NamingConverter; public class MyDbContext : DbContext { @@ -27,12 +28,12 @@ public class MyDbContext : DbContext | Naming Policy | Original | Converted | |----------------|--------------|--------------| -| PascalCase(Default) | OrderItem | OrderItem | -| CamelCase | OrderItem | orderItem | -| SnakeCaseLower | OrderItem | order_item | -| SnakeCaseUpper | OrderItem | ORDER_ITEM | -| KebabCaseLower | OrderItem | order-item | -| KebabCaseUpper | OrderItem | ORDER-ITEM | +| PascalCase(Default) | FullName | FullName | +| CamelCase | FullName | fullName | +| SnakeCaseLower | FullName | full_name | +| SnakeCaseUpper | FullName | FULL_NAME | +| KebabCaseLower | FullName | full-name | +| KebabCaseUpper | FullName | FULL-NAME | ## Github diff --git a/README.zh-CN.md b/README.zh-CN.md deleted file mode 100644 index d1fd226..0000000 --- a/README.zh-CN.md +++ /dev/null @@ -1,39 +0,0 @@ -## 介绍 - -该项目为 Entity Framework Core 提供了一个命名转换器,用于将表名和列名转换为指定的命名规则,源码参阅 System.Text.Json 库中命名策略实现,目前是预览版,可能会有一些问题,欢迎大家提出建议,让我们改进它。 - -## 安装 -```shell -dotnet add package EfCore.NamingConverter -``` -## 使用 - -在 DbContext 类中使用命名转换器是微软默认的配置方式,只需要在 ConfigureConventions 方法中添加命名转换器即可,下一个版本考虑提供 DbContextOptions 配置项,方便配置。 - -```csharp - -public class MyDbContext : DbContext -{ - protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) - { - configurationBuilder.AddNamingConventions(NamingPolicy.SnakeCaseLower); - } -} -``` - -## 规则 - -| Naming Policy | Original | Converted | -|----------------|--------------|--------------| -| PascalCase(Default) | OrderItem | OrderItem | -| CamelCase | OrderItem | orderItem | -| SnakeCaseLower | OrderItem | order_item | -| SnakeCaseUpper | OrderItem | ORDER_ITEM | -| KebabCaseLower | OrderItem | order-item | -| KebabCaseUpper | OrderItem | ORDER-ITEM | - -## Github - -https://github.com/bit365/efcore-naming-converter - -如果你对这个项目感兴趣,欢迎 star 和 fork,也欢迎提 issue 和 pr,让我们一起改进这个项目。