From 72638070af7c1cb91f1fe773a808130cb242b906 Mon Sep 17 00:00:00 2001 From: spacekey Date: Tue, 25 Oct 2022 09:37:17 +0900 Subject: [PATCH] Add parameter less constructor to EntityFrameworkValueConverter --- UnitGenerator.sln | 11 ++++++-- sandbox/FileGenerate/FileGenerate.csproj | 2 +- src/EntityFrameworkApp/DbContext.cs | 27 +++++++++++++++++++ .../EntityFrameworkApp.csproj | 18 +++++++++++++ src/EntityFrameworkApp/Program.cs | 10 +++++++ src/UnitGenerator/CodeTemplate.cs | 14 +++++++--- src/UnitGenerator/CodeTemplate.tt | 7 +++++ .../UnitGenerator.Tests.csproj | 2 +- 8 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/EntityFrameworkApp/DbContext.cs create mode 100644 src/EntityFrameworkApp/EntityFrameworkApp.csproj create mode 100644 src/EntityFrameworkApp/Program.cs diff --git a/UnitGenerator.sln b/UnitGenerator.sln index 2873bc9..24ed89c 100644 --- a/UnitGenerator.sln +++ b/UnitGenerator.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30711.63 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitGenerator", "src\UnitGenerator\UnitGenerator.csproj", "{208D16B4-5904-4E06-8A41-D06C3FE7293E}" EndProject @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitGenerator.Tests", "test EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileGenerate", "sandbox\FileGenerate\FileGenerate.csproj", "{F8353A7A-290E-41D7-A6F8-8D8DBDD44433}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkApp", "src\EntityFrameworkApp\EntityFrameworkApp.csproj", "{51AE7857-4223-40FE-AEA9-F0E64C5F8238}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Debug|Any CPU.Build.0 = Debug|Any CPU {F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Release|Any CPU.ActiveCfg = Release|Any CPU {F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Release|Any CPU.Build.0 = Release|Any CPU + {51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -48,6 +54,7 @@ Global {8AF2ABCF-38FB-4D64-A051-8735AF83E223} = {34EB4113-923D-4855-979C-A0467461B75C} {5DA06D43-A023-4464-B856-8BB42E8E4A05} = {187FBF64-D2AA-444D-AFB1-CE999BC6AD34} {F8353A7A-290E-41D7-A6F8-8D8DBDD44433} = {34EB4113-923D-4855-979C-A0467461B75C} + {51AE7857-4223-40FE-AEA9-F0E64C5F8238} = {34EB4113-923D-4855-979C-A0467461B75C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A64DF779-7829-414F-9E6E-3AF349486508} diff --git a/sandbox/FileGenerate/FileGenerate.csproj b/sandbox/FileGenerate/FileGenerate.csproj index 5b9182c..96575cf 100644 --- a/sandbox/FileGenerate/FileGenerate.csproj +++ b/sandbox/FileGenerate/FileGenerate.csproj @@ -1,7 +1,7 @@  - net5.0 + net6.0 true $(ProjectDir)..\Generated diff --git a/src/EntityFrameworkApp/DbContext.cs b/src/EntityFrameworkApp/DbContext.cs new file mode 100644 index 0000000..db5a1dd --- /dev/null +++ b/src/EntityFrameworkApp/DbContext.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using UnitGenerator; + +namespace EntityFrameworkApp; + +[UnitOf(typeof(int), + UnitGenerateOptions.ParseMethod | + UnitGenerateOptions.EntityFrameworkValueConverter)] +public readonly partial struct UserId { } + +public class User +{ + public UserId UserId { get; set; } +} + +public class SampleDbContext : DbContext +{ + public SampleDbContext() { } + public SampleDbContext(DbContextOptions options) : base(options) { } + + public DbSet Users { get; set; } = default!; + + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) + { + configurationBuilder.Properties().HaveConversion(); + } +} diff --git a/src/EntityFrameworkApp/EntityFrameworkApp.csproj b/src/EntityFrameworkApp/EntityFrameworkApp.csproj new file mode 100644 index 0000000..e68d3c8 --- /dev/null +++ b/src/EntityFrameworkApp/EntityFrameworkApp.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/src/EntityFrameworkApp/Program.cs b/src/EntityFrameworkApp/Program.cs new file mode 100644 index 0000000..94614f6 --- /dev/null +++ b/src/EntityFrameworkApp/Program.cs @@ -0,0 +1,10 @@ +using EntityFrameworkApp; +using Microsoft.EntityFrameworkCore; + +var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase("SampleDb") + .Options; +var context = new SampleDbContext(options); +var user = context.Users.SingleOrDefault(x => x.UserId == new UserId(1)); + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/src/UnitGenerator/CodeTemplate.cs b/src/UnitGenerator/CodeTemplate.cs index c231b0b..342e667 100644 --- a/src/UnitGenerator/CodeTemplate.cs +++ b/src/UnitGenerator/CodeTemplate.cs @@ -1,10 +1,10 @@ // ------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// このコードはツールによって生成されました。 +// ランタイム バージョン: 17.0.0.0 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// このファイルへの変更は、正しくない動作の原因になる可能性があり、 +// コードが再生成されると失われます。 // // ------------------------------------------------------------------------------ namespace UnitGenerator @@ -516,6 +516,12 @@ public virtual string TransformText() this.Write(this.ToStringHelper.ToStringWithCulture(Type)); this.Write(">\r\n {\r\n public "); this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("ValueConverter()\r\n : base(\r\n convertToProvi" + + "derExpression: x => x.value,\r\n convertFromProviderExpress" + + "ion: x => new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("(x))\r\n {\r\n }\r\n\r\n public "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); this.Write(@"ValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( convertToProviderExpression: x => x.value, diff --git a/src/UnitGenerator/CodeTemplate.tt b/src/UnitGenerator/CodeTemplate.tt index 86f1dfe..3f92d7c 100644 --- a/src/UnitGenerator/CodeTemplate.tt +++ b/src/UnitGenerator/CodeTemplate.tt @@ -453,6 +453,13 @@ namespace <#= Namespace #> // UnitGenerateOptions.EntityFrameworkValueConverter public class <#= Name #>ValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<<#= Name #>, <#= Type #>> { + public <#= Name #>ValueConverter() + : base( + convertToProviderExpression: x => x.value, + convertFromProviderExpression: x => new <#= Name #>(x)) + { + } + public <#= Name #>ValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( convertToProviderExpression: x => x.value, diff --git a/tests/UnitGenerator.Tests/UnitGenerator.Tests.csproj b/tests/UnitGenerator.Tests/UnitGenerator.Tests.csproj index 4ddff6a..7258855 100644 --- a/tests/UnitGenerator.Tests/UnitGenerator.Tests.csproj +++ b/tests/UnitGenerator.Tests/UnitGenerator.Tests.csproj @@ -1,7 +1,7 @@  - net5.0 + net6.0 latest false