Skip to content

Commit

Permalink
Fix exception when value generation strategy is None
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Dec 2, 2020
1 parent e3923d7 commit a3176b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ protected override bool IsHandledByConvention(IProperty property, IAnnotation an
// and IdentityByDefault otherwise.
if (annotation.Name == NpgsqlAnnotationNames.ValueGenerationStrategy)
{
Debug.Assert(property.ValueGenerated == ValueGenerated.OnAdd);

// Note: both serial and identity-by-default columns are considered by-convention - we don't want
// to assume that the PostgreSQL version of the scaffolded database necessarily determines the
// version of the database that the scaffolded model will target. This makes life difficult for
Expand Down Expand Up @@ -201,8 +199,17 @@ IReadOnlyList<MethodCallCodeFragment> GenerateValueGenerationStrategy(
})
};

case NpgsqlValueGenerationStrategy.None:
return new List<MethodCallCodeFragment>
{
new(
nameof(ModelBuilder.HasAnnotation),
NpgsqlAnnotationNames.ValueGenerationStrategy,
NpgsqlValueGenerationStrategy.None)
};

default:
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(strategy.ToString());
}

T GetAndRemove<T>(string annotationName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Conventions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
Expand All @@ -25,6 +27,7 @@ public void GenerateFluentApi_value_generation()
x.Property<int>("IdentityByDefault").UseIdentityByDefaultColumn();
x.Property<int>("IdentityAlways").UseIdentityAlwaysColumn();
x.Property<int>("Serial").UseSerialColumn();
x.Property<int>("None").Metadata.SetValueGenerationStrategy(NpgsqlValueGenerationStrategy.None);
});

// Note: both serial and identity-by-default columns are considered by-convention - we don't want
Expand Down Expand Up @@ -59,6 +62,17 @@ public void GenerateFluentApi_value_generation()
.Single();
Assert.Equal(nameof(NpgsqlPropertyBuilderExtensions.UseSerialColumn), result.Method);
Assert.Equal(0, result.Arguments.Count);

property = entity.GetProperties().Single(p => p.Name == "None");
annotations = property.GetAnnotations().ToDictionary(a => a.Name, a => a);
generator.RemoveAnnotationsHandledByConventions(property, annotations);
Assert.Contains(annotations, kv => kv.Key == NpgsqlAnnotationNames.ValueGenerationStrategy);
result = generator.GenerateFluentApiCalls(property, property.GetAnnotations().ToDictionary(a => a.Name, a => a))
.Single();
Assert.Equal(nameof(PropertyBuilder.HasAnnotation), result.Method);
Assert.Collection(result.Arguments,
a => Assert.Equal(NpgsqlAnnotationNames.ValueGenerationStrategy, a),
a => Assert.Equal(NpgsqlValueGenerationStrategy.None, a));
}

[Fact]
Expand Down

0 comments on commit a3176b5

Please sign in to comment.