Skip to content

Commit 46d946b

Browse files
authored
Fix keyColumnType was not generated for DeleteDataOperations during migration.
+ Other minor fixes.
1 parent 2a267d9 commit 46d946b

File tree

10 files changed

+182
-112
lines changed

10 files changed

+182
-112
lines changed

EFCore.VisualBasic/Design/Internal/VisualBasicHelper.vb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,27 @@ Namespace Design.Internal
6060
''' </summary>
6161
Public Overridable Function Lambda(properties As IReadOnlyList(Of String), Optional lambdaIdentifier As String = Nothing) As String Implements IVisualBasicHelper.Lambda
6262

63+
NotNull(properties, NameOf(properties))
64+
NullButNotEmpty(lambdaIdentifier, NameOf(lambdaIdentifier))
65+
66+
lambdaIdentifier = If(lambdaIdentifier, "x")
67+
6368
Dim builder = New StringBuilder()
64-
builder.Append("Function(e) ")
69+
builder.
70+
Append("Function(").
71+
Append(lambdaIdentifier).
72+
Append(") ")
6573

6674
If properties.Count = 1 Then
6775
builder.
68-
Append("e.").
69-
Append(properties(0))
76+
Append(lambdaIdentifier).
77+
Append(".").
78+
Append(properties(0))
7079
Else
7180
builder.
72-
Append("New With {").
73-
AppendJoin(", ", properties.Select(Function(p) "e." & p)).
74-
Append("}")
81+
Append("New With {").
82+
AppendJoin(", ", properties.Select(Function(p) $"{lambdaIdentifier}.{p}")).
83+
Append("}")
7584
End If
7685

7786
Return builder.ToString()
@@ -85,7 +94,6 @@ Namespace Design.Internal
8594
Return Lambda(properties.Select(Function(p) p.Name).ToList(), lambdaIdentifier)
8695
End Function
8796

88-
8997
''' <summary>
9098
''' This API supports the Entity Framework Core infrastructure And Is Not intended to be used
9199
''' directly from your code. This API may change Or be removed in future releases.
@@ -125,7 +133,7 @@ Namespace Design.Internal
125133
End If
126134

127135
If type.IsNested Then
128-
Debug.Assert(type.DeclaringType IsNot Nothing, "DeclaringType is null")
136+
DebugAssert(type.DeclaringType IsNot Nothing, "DeclaringType is null")
129137
builder.
130138
Append(Reference(type.DeclaringType)).
131139
Append(".")
@@ -556,6 +564,10 @@ Namespace Design.Internal
556564
''' doing so can result in application failures when updating to a new Entity Framework Core release.
557565
''' </summary>
558566
Protected Overridable Function GetSimpleEnumValue(type As Type, name As String) As String
567+
568+
NotNull(type, NameOf(type))
569+
NotNull(name, NameOf(name))
570+
559571
Return Reference(type) & "." & name
560572
End Function
561573

@@ -567,6 +579,9 @@ Namespace Design.Internal
567579
''' </summary>
568580
Protected Overridable Function GetCompositeEnumValue(type As Type, flags As [Enum]) As String
569581

582+
NotNull(type, NameOf(type))
583+
NotNull(flags, NameOf(flags))
584+
570585
Dim allValues As New HashSet(Of [Enum])(GetFlags(flags))
571586

572587
For Each currentValue In allValues.ToList()
@@ -609,7 +624,7 @@ Namespace Design.Internal
609624
''' directly from your code. This API may change Or be removed in future releases.
610625
''' </summary>
611626
Public Overridable Function UnknownLiteral(value As Object) As String Implements IVisualBasicHelper.UnknownLiteral
612-
If value Is Nothing OrElse value Is DBNull.Value Then
627+
If value Is Nothing Then
613628
Return "Nothing"
614629
End If
615630

EFCore.VisualBasic/Migrations/Design/VisualBasicMigrationOperationGenerator.vb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,20 @@ Namespace Migrations.Design
18381838

18391839
builder.AppendLine(",")
18401840

1841+
If operation.KeyColumnTypes IsNot Nothing Then
1842+
If operation.KeyColumnTypes.Length = 1 Then
1843+
builder.
1844+
Append("keyColumnType:=").
1845+
Append(VBCode.Literal(operation.KeyColumnTypes(0)))
1846+
Else
1847+
builder.
1848+
Append("keyColumnTypes:=").
1849+
Append(VBCode.Literal(operation.KeyColumnTypes))
1850+
End If
1851+
1852+
builder.AppendLine(",")
1853+
End If
1854+
18411855
If operation.KeyValues.GetLength(0) = 1 AndAlso operation.KeyValues.GetLength(1) = 1 Then
18421856
builder.
18431857
Append("keyValue:=").
@@ -2017,7 +2031,7 @@ Namespace Migrations.Design
20172031
End Sub
20182032

20192033
Private Shared Function ToOnedimensionalArray(values As Object(,), Optional firstDimension As Boolean = False) As Object()
2020-
Debug.Assert(
2034+
DebugAssert(
20212035
values.GetLength(If(firstDimension, 1, 0)) = 1,
20222036
$"Length of dimension {If(firstDimension, 1, 0)} is not 1.")
20232037

EFCore.VisualBasic/Migrations/Design/VisualBasicSnapshotGenerator.vb

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,24 @@ Namespace Migrations.Design
7373

7474
Using stringBuilder.Indent()
7575

76+
Dim enabled As Boolean
77+
Dim useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23456", enabled) AndAlso enabled
78+
7679
' Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
7780
' providers, generating them as raw annotations instead.
78-
Dim ambiguousAnnotations =
79-
RemoveAmbiguousFluentApiAnnotations(
80-
annotations,
81-
Function(name)
82-
Return name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal) _
83-
OrElse name.EndsWith(":IdentityIncrement", StringComparison.Ordinal) _
84-
OrElse name.EndsWith(":IdentitySeed", StringComparison.Ordinal) _
85-
OrElse name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal) _
86-
OrElse name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal)
87-
End Function)
81+
Dim ambiguousAnnotations As IReadOnlyList(Of IAnnotation) = Array.Empty(Of IAnnotation)
82+
If Not useOldBehavior Then
83+
ambiguousAnnotations =
84+
RemoveAmbiguousFluentApiAnnotations(
85+
annotations,
86+
Function(name)
87+
Return name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal) _
88+
OrElse name.EndsWith(":IdentityIncrement", StringComparison.Ordinal) _
89+
OrElse name.EndsWith(":IdentitySeed", StringComparison.Ordinal) _
90+
OrElse name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal) _
91+
OrElse name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal)
92+
End Function)
93+
End If
8894

8995
For Each methodCallCodeFragment In AnnotationCodeGenerator.GenerateFluentApiCalls(model, annotations)
9096
stringBuilder.AppendLines(VBCode.Fragment(methodCallCodeFragment, vertical:=True), True)
@@ -136,21 +142,24 @@ Namespace Migrations.Design
136142
NotNull(entityTypes, NameOf(entityTypes))
137143
NotNull(stringBuilder, NameOf(stringBuilder))
138144

139-
For Each entityType In entityTypes.Where(Function(e) e.FindOwnership() Is Nothing)
145+
For Each entityType In entityTypes.Where(Function(e) Not e.HasDefiningNavigation() AndAlso
146+
e.FindOwnership() Is Nothing)
140147
stringBuilder.AppendLine()
141148
GenerateEntityType(builderName, entityType, stringBuilder)
142149
Next
143150

144151
For Each entityType In entityTypes.Where(
145-
Function(e) e.FindOwnership() Is Nothing AndAlso
152+
Function(e) Not e.HasDefiningNavigation() AndAlso
153+
e.FindOwnership() Is Nothing AndAlso
146154
(e.GetDeclaredForeignKeys().Any() OrElse e.GetDeclaredReferencingForeignKeys().Any(Function(fk) fk.IsOwnership)))
147155

148156
stringBuilder.AppendLine()
149157
GenerateEntityTypeRelationships(builderName, entityType, stringBuilder)
150158
Next
151159

152160
For Each entityType In entityTypes.Where(
153-
Function(e) e.FindOwnership() Is Nothing AndAlso
161+
Function(e) Not e.HasDefiningNavigation() AndAlso
162+
e.FindOwnership() Is Nothing AndAlso
154163
e.GetDeclaredNavigations().Any(Function(n) Not n.IsOnDependent AndAlso Not n.ForeignKey.IsOwnership))
155164

156165
stringBuilder.AppendLine()
@@ -176,21 +185,10 @@ Namespace Migrations.Design
176185
Dim ownership = entityType.FindOwnership()
177186
Dim ownerNavigation = ownership?.PrincipalToDependent.Name
178187

179-
Dim GetOwnedName = Function(Type As ITypeBase, simpleName As String, ownershipNavigation As String) As String
180-
Return Type.Name & "." & ownershipNavigation & "#" & simpleName
181-
End Function
182-
183-
Dim entityTypeName = entityType.Name
184-
If ownerNavigation IsNot Nothing AndAlso
185-
entityType.HasSharedClrType AndAlso
186-
entityTypeName = GetOwnedName(ownership.PrincipalEntityType, entityType.ClrType.ShortDisplayName(), ownerNavigation) Then
187-
entityTypeName = entityType.ClrType.DisplayName()
188-
End If
189-
190188
stringBuilder.
191189
Append(builderName).
192190
Append(If(ownerNavigation IsNot Nothing, If(ownership.IsUnique, ".OwnsOne(", ".OwnsMany("), ".Entity(")).
193-
Append(VBCode.Literal(entityTypeName))
191+
Append(VBCode.Literal(entityType.Name))
194192

195193
If ownerNavigation IsNot Nothing Then
196194
stringBuilder.
@@ -200,7 +198,7 @@ Namespace Migrations.Design
200198

201199
If builderName.StartsWith("b", StringComparison.Ordinal) Then
202200
Dim counter = 1
203-
If builderName.Length > 1 AndAlso Integer.TryParse(builderName.Substring(1, builderName.Length - 1), counter) Then
201+
If builderName.Length > 1 AndAlso Integer.TryParse(builderName.Substring(1), counter) Then
204202
counter += 1
205203
End If
206204

@@ -231,7 +229,10 @@ Namespace Migrations.Design
231229

232230
If ownerNavigation IsNot Nothing Then
233231
GenerateRelationships(builderName, entityType, stringBuilder)
234-
GenerateNavigations(builderName, entityType.GetDeclaredNavigations().Where(Function(n) Not n.IsOnDependent AndAlso Not n.ForeignKey.IsOwnership), stringBuilder)
232+
GenerateNavigations(
233+
builderName, entityType.GetDeclaredNavigations().
234+
Where(Function(n) Not n.IsOnDependent AndAlso
235+
Not n.ForeignKey.IsOwnership), stringBuilder)
235236
End If
236237

237238
GenerateData(builderName, entityType.GetProperties(), entityType.GetSeedData(providerValues:=True), stringBuilder)
@@ -469,10 +470,10 @@ Namespace Migrations.Design
469470

470471
Dim firstProperty = True
471472
For Each [property] In properties
472-
If Not firstProperty Then
473-
stringBuilder.AppendLine()
474-
Else
473+
If firstProperty Then
475474
firstProperty = False
475+
Else
476+
stringBuilder.AppendLine()
476477
End If
477478

478479
GenerateProperty(builderName, [property], stringBuilder)
@@ -497,12 +498,12 @@ Namespace Migrations.Design
497498
Dim clrType = If(FindValueConverter([property])?.ProviderClrType.MakeNullable([property].IsNullable), [property].ClrType)
498499

499500
stringBuilder.
500-
Append(builderName).
501-
Append(".Property(Of ").
502-
Append(VBCode.Reference(clrType)).
503-
Append(")(").
504-
Append(VBCode.Literal([property].Name)).
505-
Append(")")
501+
Append(builderName).
502+
Append(".Property(Of ").
503+
Append(VBCode.Reference(clrType)).
504+
Append(")(").
505+
Append(VBCode.Literal([property].Name)).
506+
Append(")")
506507

507508
Using stringBuilder.Indent()
508509
If [property].IsConcurrencyToken Then
@@ -577,18 +578,25 @@ Namespace Migrations.Design
577578
GenerateFluentApiForDefaultValue([property], stringBuilder)
578579
annotations.Remove(RelationalAnnotationNames.DefaultValue)
579580

581+
Dim enabled As Boolean
582+
Dim useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23456", enabled) AndAlso enabled
583+
580584
' Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
581585
' providers, generating them as raw annotations instead.
582-
Dim ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
583-
annotations,
584-
Function(name)
585-
Return name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal) _
586-
OrElse name.EndsWith(":IdentityIncrement", StringComparison.Ordinal) _
587-
OrElse name.EndsWith(":IdentitySeed", StringComparison.Ordinal) _
588-
OrElse name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal) _
589-
OrElse name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal)
590-
End Function
591-
)
586+
Dim ambiguousAnnotations As IReadOnlyList(Of IAnnotation) = Array.Empty(Of IAnnotation)
587+
588+
If Not useOldBehavior Then
589+
ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
590+
annotations,
591+
Function(name)
592+
Return name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal) _
593+
OrElse name.EndsWith(":IdentityIncrement", StringComparison.Ordinal) _
594+
OrElse name.EndsWith(":IdentitySeed", StringComparison.Ordinal) _
595+
OrElse name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal) _
596+
OrElse name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal)
597+
End Function
598+
)
599+
End If
592600

593601
For Each methodCallCodeFragment In AnnotationCodeGenerator.GenerateFluentApiCalls([property], annotations)
594602
stringBuilder.
@@ -764,15 +772,25 @@ Namespace Migrations.Design
764772
Protected Overridable Sub GenerateIndexAnnotations(index As IIndex,
765773
stringBuilder As IndentedStringBuilder)
766774

775+
NotNull(index, NameOf(index))
776+
NotNull(stringBuilder, NameOf(stringBuilder))
777+
767778
Dim annotations = AnnotationCodeGenerator.
768779
FilterIgnoredAnnotations(index.GetAnnotations()).
769780
ToDictionary(Function(a) a.Name, Function(a) a)
770781

782+
Dim enabled As Boolean
783+
Dim useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23456", enabled) AndAlso enabled
784+
771785
' Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
772786
' providers, generating them as raw annotations instead.
773-
Dim ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
787+
Dim ambiguousAnnotations As IReadOnlyList(Of IAnnotation) = Array.Empty(Of IAnnotation)
788+
789+
If Not useOldBehavior Then
790+
ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
774791
annotations,
775792
Function(name) name.EndsWith(":Include", StringComparison.Ordinal))
793+
End If
776794

777795
For Each methodCallCodeFragment In AnnotationCodeGenerator.GenerateFluentApiCalls(index, annotations)
778796
stringBuilder.
@@ -841,7 +859,7 @@ Namespace Migrations.Design
841859
' Issue #23173
842860
stringBuilder.Append(", excludedFromMigrations:=true")
843861
Else
844-
stringBuilder.Append(", Function(t) t.ExcludeFromMigrations()")
862+
stringBuilder.Append(", Sub(t) t.ExcludeFromMigrations()")
845863
End If
846864
End If
847865

EFCore.VisualBasic/Scaffolding/Internal/VisualBasicDbContextGenerator.vb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ Namespace Scaffolding.Internal
415415

416416
Dim explicitSchema As Boolean = schema1 IsNot Nothing AndAlso schema1 <> defaultSchema
417417
Dim explicitTable As Boolean = explicitSchema OrElse tableName1 IsNot Nothing AndAlso tableName1 <> entityType.GetDbSetName()
418+
418419
If explicitTable Then
419420
Dim parameterString = _code.Literal(tableName1)
420421
If explicitSchema Then
@@ -509,13 +510,13 @@ Namespace Scaffolding.Internal
509510

510511
Dim precision = prop.GetPrecision()
511512
Dim scale = prop.GetScale()
512-
If precision IsNot Nothing AndAlso scale IsNot Nothing AndAlso scale <> 0 Then
513+
If precision.HasValue AndAlso scale.HasValue AndAlso scale <> 0 Then
513514
lines.Add($".{NameOf(PropertyBuilder.HasPrecision)}({_code.Literal(precision.Value)}, {_code.Literal(scale.Value)})")
514-
ElseIf precision IsNot Nothing Then
515+
ElseIf precision.HasValue Then
515516
lines.Add($".{NameOf(PropertyBuilder.HasPrecision)}({_code.Literal(precision.Value)})")
516517
End If
517518

518-
If prop.IsUnicode() IsNot Nothing Then
519+
If prop.IsUnicode().HasValue Then
519520
lines.Add($".{NameOf(PropertyBuilder.IsUnicode)}({(If(prop.IsUnicode() = False, "false", ""))})")
520521
End If
521522
End If
@@ -615,9 +616,9 @@ Namespace Scaffolding.Internal
615616
End If
616617

617618
lines.Add(
618-
$".{NameOf(ReferenceReferenceBuilder.HasForeignKey)}" &
619-
If(foreignKey.IsUnique, $"(Of {foreignKey.DeclaringEntityType.Name})", "") &
620-
$"({_code.Lambda(foreignKey.Properties, "d")})")
619+
$".{NameOf(ReferenceReferenceBuilder.HasForeignKey)}" &
620+
If(foreignKey.IsUnique, $"(Of {foreignKey.DeclaringEntityType.Name})", "") &
621+
$"({_code.Lambda(foreignKey.Properties, "d")})")
621622

622623
Dim defaultOnDeleteAction = If(foreignKey.IsRequired, DeleteBehavior.Cascade, DeleteBehavior.ClientSetNull)
623624

0 commit comments

Comments
 (0)