Skip to content

Commit

Permalink
fixup! Differentiate between a sequence's database schema and model s…
Browse files Browse the repository at this point in the history
…chema

Fix the tests
  • Loading branch information
bricelam committed Sep 1, 2022
1 parent cd0f4ba commit 138642b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Internal;
Expand Down Expand Up @@ -328,7 +329,8 @@ private string CreateModelBuilder(
mainBuilder,
methodBuilder,
namespaces,
variables);
variables,
nullable);

foreach (var typeConfiguration in model.GetTypeMappingConfigurations())
{
Expand Down Expand Up @@ -471,7 +473,7 @@ private string GenerateEntityType(IEntityType entityType, string @namespace, str
CreateSkipNavigation(navigation, navigationNumber++, mainBuilder, methodBuilder, namespaces, className, nullable);
}

CreateAnnotations(entityType, mainBuilder, methodBuilder, namespaces, className);
CreateAnnotations(entityType, mainBuilder, methodBuilder, namespaces, className, nullable);
}

mainBuilder.AppendLine("}");
Expand Down Expand Up @@ -522,7 +524,8 @@ private void CreateEntityType(
mainBuilder,
methodBuilder,
namespaces,
variables);
variables,
nullable);

Create(entityType, parameters);

Expand Down Expand Up @@ -1150,7 +1153,8 @@ private void CreateForeignKey(
mainBuilder,
methodBuilder,
namespaces,
variables);
variables,
nullable);

var navigation = foreignKey.DependentToPrincipal;
if (navigation != null)
Expand Down Expand Up @@ -1249,7 +1253,8 @@ private void CreateSkipNavigation(
mainBuilder,
methodBuilder,
namespaces,
variables);
variables,
nullable);

mainBuilder
.Append("var ").Append(navigationVariable).Append(" = ")
Expand Down Expand Up @@ -1352,7 +1357,8 @@ private void CreateAnnotations(
IndentedStringBuilder mainBuilder,
IndentedStringBuilder methodBuilder,
SortedSet<string> namespaces,
string className)
string className,
bool nullable)
{
mainBuilder.AppendLine()
.Append("public static void CreateAnnotations")
Expand All @@ -1373,7 +1379,8 @@ private void CreateAnnotations(
mainBuilder,
methodBuilder,
namespaces,
variables));
variables,
nullable));

mainBuilder
.AppendLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ public override void Generate(IModel model, CSharpRuntimeAnnotationCodeGenerator
{
parameters.Namespaces.Add(typeof(SortedDictionary<,>).Namespace!);
var sequencesVariable = Dependencies.CSharpHelper.Identifier("sequences", parameters.ScopeVariables, capitalize: false);
parameters.MainBuilder
.Append("var ").Append(sequencesVariable).AppendLine(" = new SortedDictionary<(string, string), ISequence>();");
var mainBuilder = parameters.MainBuilder;
mainBuilder.Append("var ").Append(sequencesVariable).Append(" = new SortedDictionary<(string, string");

if (parameters.UseNullableReferenceTypes)
{
mainBuilder.Append("?");
}

mainBuilder.AppendLine("), ISequence>();");

foreach (var sequencePair in sequences)
{
Expand Down Expand Up @@ -279,7 +286,7 @@ private void Create(ISequence sequence, string sequencesVariable, CSharpRuntimeA
.Append("maxValue: ").Append(code.Literal(sequence.MaxValue));
}

if (sequence.ModelSchema is null)
if (sequence.ModelSchema is null && sequence.Schema is not null)
{
mainBuilder.AppendLine(",")
.Append("modelSchemaIsNull: ").Append(code.Literal(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public CSharpRuntimeAnnotationCodeGeneratorParameters(
IndentedStringBuilder mainBuilder,
IndentedStringBuilder methodBuilder,
ISet<string> namespaces,
ISet<string> scopeVariables)
ISet<string> scopeVariables,
bool nullable)
{
TargetName = targetName;
ClassName = className;
MainBuilder = mainBuilder;
MethodBuilder = methodBuilder;
Namespaces = namespaces;
ScopeVariables = scopeVariables;
UseNullableReferenceTypes = nullable;
}

/// <summary>
Expand Down Expand Up @@ -78,4 +80,10 @@ public CSharpRuntimeAnnotationCodeGeneratorParameters(
/// Indicates whether the given annotations are runtime annotations.
/// </summary>
public bool IsRuntime { get; init; }

/// <summary>
/// Gets or sets a value indicating whther nullable reference types are enabled.
/// </summary>
/// <value>A value indicating whther nullable reference types are enabled.</value>
public bool UseNullableReferenceTypes { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2410,14 +2410,15 @@ partial void Initialize()
PrincipalBaseEntityType.CreateAnnotations(principalBase);
PrincipalDerivedEntityType.CreateAnnotations(principalDerived);
var sequences = new SortedDictionary<(string, string), ISequence>();
var sequences = new SortedDictionary<(string, string?), ISequence>();
var principalBaseSequence = new RuntimeSequence(
""PrincipalBaseSequence"",
this,
typeof(long),
schema: ""TPC"");
schema: ""TPC"",
modelSchemaIsNull: true);
sequences[(""PrincipalBaseSequence"", ""TPC"")] = principalBaseSequence;
sequences[(""PrincipalBaseSequence"", null)] = principalBaseSequence;
AddAnnotation(""Relational:Sequences"", sequences);
AddAnnotation(""Relational:DefaultSchema"", ""TPC"");
Expand Down Expand Up @@ -2914,6 +2915,9 @@ public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
principalDerived
},
model.GetEntityTypes());
var principalBaseSequence = model.FindSequence("PrincipalBaseSequence");
Assert.Equal("TPC", principalBaseSequence.Schema);
},
typeof(SqlServerNetTopologySuiteDesignTimeServices));

Expand Down

0 comments on commit 138642b

Please sign in to comment.