Skip to content

Commit

Permalink
Fix ValueGeneratedOnUpdate in scaffolding
Browse files Browse the repository at this point in the history
And add some tests.

Fixes #18579
  • Loading branch information
roji committed Oct 29, 2019
1 parent 23a8f0b commit f28c7db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,28 +703,16 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
if (((IConventionProperty)property).GetValueGeneratedConfigurationSource().HasValue
&& RelationalValueGenerationConvention.GetValueGenerated(property) != valueGenerated)
{
string methodName;
switch (valueGenerated)
var methodName = valueGenerated switch
{
case ValueGenerated.OnAdd:
methodName = nameof(PropertyBuilder.ValueGeneratedOnAdd);
break;

case ValueGenerated.OnAddOrUpdate:
isRowVersion = property.IsConcurrencyToken;
methodName = isRowVersion
? nameof(PropertyBuilder.IsRowVersion)
: nameof(PropertyBuilder.ValueGeneratedOnAddOrUpdate);
break;

case ValueGenerated.Never:
methodName = nameof(PropertyBuilder.ValueGeneratedNever);
break;

default:
methodName = "";
break;
}
ValueGenerated.OnAdd => nameof(PropertyBuilder.ValueGeneratedOnAdd),
ValueGenerated.OnAddOrUpdate => property.IsConcurrencyToken
? nameof(PropertyBuilder.IsRowVersion)
: nameof(PropertyBuilder.ValueGeneratedOnAddOrUpdate),
ValueGenerated.OnUpdate => nameof(PropertyBuilder.ValueGeneratedOnUpdate),
ValueGenerated.Never => nameof(PropertyBuilder.ValueGeneratedNever),
_ => throw new InvalidOperationException($"Unhandled enum value '{nameof(ValueGenerated)}.{valueGenerated}'")
};

lines.Add($".{methodName}()");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,41 @@ public void ModelSameNamespaceDbContext_works()
);
}

[ConditionalFact]
public void ValueGenerated_works()
{
Test(
modelBuilder => modelBuilder.Entity(
"Entity",
x =>
{
x.Property<int>("ValueGeneratedOnAdd").ValueGeneratedOnAdd();
x.Property<int>("ValueGeneratedOnAddOrUpdate").ValueGeneratedOnAddOrUpdate();
x.Property<int>("ConcurrencyToken").IsConcurrencyToken();
x.Property<int>("ValueGeneratedOnUpdate").ValueGeneratedOnUpdate();
x.Property<int>("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()
Expand Down

0 comments on commit f28c7db

Please sign in to comment.