Skip to content

Commit 78b40ea

Browse files
authored
Fix scaffolding modelBuilder annotations (#35)
1 parent 8af8eb3 commit 78b40ea

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

EFCore.VisualBasic/Scaffolding/Internal/VisualBasicDbContextGenerator.vb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,26 @@ Namespace Scaffolding.Internal
218218

219219
Dim lines As New List(Of String)
220220

221-
lines.AddRange(
222-
_annotationCodeGenerator.GenerateFluentApiCalls(model, annotations).
223-
Select(Function(m) _code.Fragment(m)).
224-
Concat(GenerateAnnotations(annotations.Values)))
221+
lines.AddRange(_annotationCodeGenerator.GenerateFluentApiCalls(model, annotations).
222+
Select(Function(m) _code.Fragment(m)).
223+
Concat(GenerateAnnotations(annotations.Values)))
225224

226225
If lines.Any() Then
227226
Using _sb.Indent()
228-
_sb.AppendLine()
229-
_sb.Append("modelBuilder" & lines(0))
230-
231-
Using _sb.Indent()
232-
For Each line In lines.Skip(1)
233-
_sb.AppendLine()
234-
_sb.Append(line)
235-
Next
236-
End Using
227+
_sb.Append("modelBuilder")
228+
229+
If lines.Count = 1 Then
230+
_sb.Append(lines(0))
231+
Else
232+
Using _sb.Indent()
233+
For Each line In lines
234+
_sb.AppendLine(".").
235+
Append(line.TrimStart("."c))
236+
Next
237+
End Using
238+
End If
237239
End Using
240+
_sb.AppendLine()
238241
End If
239242

240243
Using _sb.Indent()
@@ -333,11 +336,9 @@ Namespace Scaffolding.Internal
333336
Private Sub AppendMultiLineFluentApi(entityType As IEntityType, lines As IList(Of String))
334337
If lines.Count <= 0 Then Exit Sub
335338

336-
If lines.Count > 1 Then
337-
For i = 1 To lines.Count - 1
338-
lines(i) = If(lines(i).StartsWith(".", StringComparison.InvariantCulture), lines(i).Remove(0, 1), lines(i))
339-
Next
340-
End If
339+
For i = 1 To lines.Count - 1
340+
lines(i) = lines(i).TrimStart("."c)
341+
Next
341342

342343
InitializeEntityTypeBuilder(entityType)
343344

@@ -347,16 +348,16 @@ Namespace Scaffolding.Internal
347348

348349
Using _sb.Indent()
349350
For Each line In lines.Skip(1)
350-
_sb.AppendLine(".")
351-
_sb.Append(line)
351+
_sb.AppendLine(".").
352+
Append(line)
352353
Next
353354
End Using
354355
_sb.AppendLine()
355356
End Using
356357
End Sub
357358

358-
Private Sub GenerateKey(akey As IKey, entityType As IEntityType, useDataAnnotations As Boolean)
359-
If akey Is Nothing Then
359+
Private Sub GenerateKey(aKey As IKey, entityType As IEntityType, useDataAnnotations As Boolean)
360+
If aKey Is Nothing Then
360361
If Not useDataAnnotations Then
361362
Dim line As New List(Of String) From {
362363
$".{NameOf(EntityTypeBuilder.HasNoKey)}()"}
@@ -368,19 +369,19 @@ Namespace Scaffolding.Internal
368369
End If
369370

370371
Dim annotations = _annotationCodeGenerator.
371-
FilterIgnoredAnnotations(akey.GetAnnotations()).
372+
FilterIgnoredAnnotations(aKey.GetAnnotations()).
372373
ToDictionary(Function(a) a.Name, Function(a) a)
373374

374-
_annotationCodeGenerator.RemoveAnnotationsHandledByConventions(akey, annotations)
375+
_annotationCodeGenerator.RemoveAnnotationsHandledByConventions(aKey, annotations)
375376

376-
Dim explicitName As Boolean = akey.GetName() <> akey.GetDefaultName()
377+
Dim explicitName As Boolean = aKey.GetName() <> aKey.GetDefaultName()
377378
annotations.Remove(RelationalAnnotationNames.Name)
378379

379-
If akey.Properties.Count = 1 AndAlso annotations.Count = 0 Then
380-
If TypeOf akey Is Key Then
381-
Dim concreteKey = DirectCast(akey, Key)
380+
If aKey.Properties.Count = 1 AndAlso annotations.Count = 0 Then
381+
If TypeOf aKey Is Key Then
382+
Dim concreteKey = DirectCast(aKey, Key)
382383

383-
If akey.Properties.SequenceEqual(
384+
If aKey.Properties.SequenceEqual(
384385
KeyDiscoveryConvention.DiscoverKeyProperties(
385386
concreteKey.DeclaringEntityType,
386387
concreteKey.DeclaringEntityType.GetProperties())) Then
@@ -394,19 +395,18 @@ Namespace Scaffolding.Internal
394395
End If
395396

396397
Dim lines As New List(Of String) From {
397-
$".{NameOf(EntityTypeBuilder.HasKey)}({_code.Lambda(akey.Properties, "e")})"}
398+
$".{NameOf(EntityTypeBuilder.HasKey)}({_code.Lambda(aKey.Properties, "e")})"}
398399

399400
If explicitName Then
400-
lines.Add(
401-
$".{NameOf(RelationalKeyBuilderExtensions.HasName)}({_code.Literal(akey.GetName())})")
401+
lines.Add($".{NameOf(RelationalKeyBuilderExtensions.HasName)}({_code.Literal(aKey.GetName())})")
402402
End If
403403

404404
lines.AddRange(
405-
_annotationCodeGenerator.GenerateFluentApiCalls(akey, annotations).
405+
_annotationCodeGenerator.GenerateFluentApiCalls(aKey, annotations).
406406
Select(Function(m) _code.Fragment(m)).
407407
Concat(GenerateAnnotations(annotations.Values)))
408408

409-
AppendMultiLineFluentApi(akey.DeclaringEntityType, lines)
409+
AppendMultiLineFluentApi(aKey.DeclaringEntityType, lines)
410410
End Sub
411411

412412
Private Sub GenerateTableName(entityType As IEntityType)

Test/EFCore.VisualBasic.Test/Scaffolding/Internal/VisualBasicDbContextGeneratorTest.vb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,32 @@ End Namespace
513513
End Sub)
514514
End Sub
515515

516+
<ConditionalFact>
517+
Public Sub modelBuilder_annotation_generated_correctly()
518+
Test(
519+
Sub(modelBuilder) modelBuilder.HasAnnotation("TestAnnotation1", 1),
520+
New ModelCodeGenerationOptions,
521+
Sub(code) Assert.Contains("modelBuilder.HasAnnotation(""TestAnnotation1"", 1)", code.ContextFile.Code),
522+
Sub(model)
523+
Assert.Equal(1, model.FindAnnotation("TestAnnotation1").Value)
524+
End Sub)
525+
End Sub
526+
527+
<ConditionalFact>
528+
Public Sub modelBuilder_annotations_generated_correctly()
529+
Test(
530+
Sub(modelBuilder) modelBuilder.HasAnnotation("TestAnnotation1", 1).HasAnnotation("TestAnnotation2", CByte(2)),
531+
New ModelCodeGenerationOptions,
532+
Sub(code) Assert.Contains(
533+
"modelBuilder.
534+
HasAnnotation(""TestAnnotation1"", 1).
535+
HasAnnotation(""TestAnnotation2"", CByte(2))", code.ContextFile.Code),
536+
Sub(model)
537+
Assert.Equal(1, model.FindAnnotation("TestAnnotation1").Value)
538+
Assert.Equal(CByte(2), model.FindAnnotation("TestAnnotation2").Value)
539+
End Sub)
540+
End Sub
541+
516542
<ConditionalFact>
517543
Public Sub Sequence_works()
518544
Test(

0 commit comments

Comments
 (0)