From 1b86c4a277bab7f9050b1a73e76313aa7245516c Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Mon, 28 Oct 2019 17:10:30 +0100 Subject: [PATCH] Fix ValueGeneratedOnUpdate in scaffolding And add some tests. Fixes #18579 --- .../Internal/CSharpDbContextGenerator.cs | 4 +++ .../Internal/CSharpDbContextGeneratorTest.cs | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs index 13a1f49dbe4..407db157cf1 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs @@ -717,6 +717,10 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations) : nameof(PropertyBuilder.ValueGeneratedOnAddOrUpdate); break; + case ValueGenerated.OnUpdate: + methodName = nameof(PropertyBuilder.ValueGeneratedOnUpdate); + break; + case ValueGenerated.Never: methodName = nameof(PropertyBuilder.ValueGeneratedNever); break; diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs index 31dc3807ef3..6ffedff1dd0 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs @@ -229,6 +229,41 @@ public void ModelSameNamespaceDbContext_works() ); } + [ConditionalFact] + public void ValueGenerated_works() + { + Test( + modelBuilder => modelBuilder.Entity( + "Entity", + x => + { + x.Property("ValueGeneratedOnAdd").ValueGeneratedOnAdd(); + x.Property("ValueGeneratedOnAddOrUpdate").ValueGeneratedOnAddOrUpdate(); + x.Property("ConcurrencyToken").IsConcurrencyToken(); + x.Property("ValueGeneratedOnUpdate").ValueGeneratedOnUpdate(); + x.Property("ValueGeneratedNever").ValueGeneratedNever(); + }), + new ModelCodeGenerationOptions(), + code => + { + Assert.Contains(@"Property(e => e.ValueGeneratedOnAdd) + .ValueGeneratedOnAdd()", code.ContextFile.Code); + Assert.Contains("Property(e => e.ValueGeneratedOnAddOrUpdate).ValueGeneratedOnAddOrUpdate()", code.ContextFile.Code); + Assert.Contains("Property(e => e.ConcurrencyToken).IsConcurrencyToken()", code.ContextFile.Code); + Assert.Contains("Property(e => e.ValueGeneratedOnUpdate).ValueGeneratedOnUpdate()", code.ContextFile.Code); + Assert.Contains("Property(e => e.ValueGeneratedNever).ValueGeneratedNever()", code.ContextFile.Code); + }, + model => + { + var entity = model.FindEntityType("TestNamespace.Entity"); + Assert.Equal(ValueGenerated.OnAdd, entity.GetProperty("ValueGeneratedOnAdd").ValueGenerated); + Assert.Equal(ValueGenerated.OnAddOrUpdate, entity.GetProperty("ValueGeneratedOnAddOrUpdate").ValueGenerated); + Assert.True(entity.GetProperty("ConcurrencyToken").IsConcurrencyToken); + Assert.Equal(ValueGenerated.OnUpdate, entity.GetProperty("ValueGeneratedOnUpdate").ValueGenerated); + Assert.Equal(ValueGenerated.Never, entity.GetProperty("ValueGeneratedNever").ValueGenerated); + }); + } + private class TestCodeGeneratorPlugin : ProviderCodeGeneratorPlugin { public override MethodCallCodeFragment GenerateProviderOptions()