Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ValueGeneratedOnUpdate in scaffolding #18619

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason here? We agreed to use it except for complex situations, and this seems as simple as it gets. If we don't use it here we basically don't use it anywhere, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 line ternary as a return of one of the statement. That is complex enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how I understand the decision made by the team, and basically invalidates the use of this construct for the vast majority of cases. This isn't extremely important but maybe another opinion would help clarify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me. Still readable.

{
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