Skip to content

Commit

Permalink
Scaffolding: Clean up code generation tests
Browse files Browse the repository at this point in the history
The code generation tests use a model builder which produces a slightly different model than the scaffolding model factory. This compensates for some of those differences to make the test assertions closer to what the end-to-end scaffolding pipeline generates.
  • Loading branch information
bricelam committed Jan 17, 2023
1 parent 6ccf4dd commit 03bb7b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 97 deletions.
1 change: 1 addition & 0 deletions src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs
Expand Up @@ -66,6 +66,7 @@ public static bool IsLeftNavigation(this ISkipNavigation skipNavigation)
/// <returns>The property name.</returns>
public static string GetDbSetName(this IReadOnlyEntityType entityType)
=> (string?)entityType[ScaffoldingAnnotationNames.DbSetName]
?? entityType.GetTableName()
?? entityType.ShortName();

/// <summary>
Expand Down
Expand Up @@ -303,6 +303,22 @@ protected override bool IsHandledByConvention(IModel model, IAnnotation annotati
&& (SqlServerValueGenerationStrategy)annotation.Value! == SqlServerValueGenerationStrategy.IdentityColumn;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override bool IsHandledByConvention(IProperty property, IAnnotation annotation)
{
if (annotation.Name == SqlServerAnnotationNames.ValueGenerationStrategy)
{
return (SqlServerValueGenerationStrategy)annotation.Value! == property.DeclaringEntityType.Model.GetValueGenerationStrategy();
}

return base.IsHandledByConvention(property, annotation);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Expand Up @@ -394,12 +394,7 @@ public Task ValueGenerated_works()
new ModelCodeGenerationOptions(),
code =>
{
AssertContains(
"""
Property(e => e.ValueGeneratedOnAdd)
.ValueGeneratedOnAdd()
""",
code.ContextFile.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);
Expand Down Expand Up @@ -593,8 +588,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasIndex(e => new { e.B, e.C }, "IndexOnBAndC")
.HasFilter("Filter SQL")
.HasAnnotation("AnnotationName", "AnnotationValue");
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -663,8 +656,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasIndex(e => new { e.B, e.C }, "IndexOnBAndC")
.HasFilter("Filter SQL")
.HasAnnotation("AnnotationName", "AnnotationValue");
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -742,8 +733,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasIndex(e => new { e.X, e.Y, e.Z }, "IX_mixed").IsDescending(false, true, false);
entity.HasIndex(e => new { e.X, e.Y, e.Z }, "IX_unspecified");
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -835,8 +824,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
entity.HasIndex(e => e.DependentId, "IX_DependentEntity_DependentId").IsUnique();
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasOne(d => d.NavigationToPrincipal).WithOne(p => p.NavigationToDependent)
.HasPrincipalKey<PrincipalEntity>(p => p.PrincipalId)
.HasForeignKey<DependentEntity>(d => d.DependentId);
Expand All @@ -845,8 +832,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<PrincipalEntity>(entity =>
{
entity.HasKey(e => e.AlternateId);
entity.Property(e => e.AlternateId).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -902,7 +887,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.Property(e => e.HireDate)
.HasColumnType("date")
.HasColumnName("hiring_date");
Expand Down Expand Up @@ -1116,8 +1100,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasPeriodEnd("PeriodEnd")
.HasColumnName("PeriodEnd");
}));
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -1219,8 +1201,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
tb.HasTrigger("Trigger1");
tb.HasTrigger("Trigger2");
});
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down
Expand Up @@ -91,6 +91,7 @@ public Task TableAttribute_is_generated_for_custom_name()
b =>
{
b.ToTable("Vistas"); // Default name is "Vista" in the absence of pluralizer
b.HasAnnotation(ScaffoldingAnnotationNames.DbSetName, "Vista");
b.Property<int>("Id");
b.HasKey("Id");
});
Expand Down Expand Up @@ -447,8 +448,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<EntityWithIndexes>(entity =>
{
entity.HasIndex(e => new { e.B, e.C }, "IndexOnBAndC").HasFilter("Filter SQL");
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -521,11 +520,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity>(entity =>
{
entity.Property(e => e.PrimaryKey).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -1078,11 +1072,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -1553,11 +1542,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -1654,15 +1638,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasOne(d => d.BlogNavigation).WithMany(p => p.Posts)
.HasPrincipalKey(p => new { p.Id1, p.Id2 })
.HasForeignKey(d => new { d.BlogId1, d.BlogId2 });
Expand Down Expand Up @@ -1791,18 +1768,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasOne(d => d.Color).WithMany(p => p.Cars)
.HasPrincipalKey(p => p.ColorCode)
.HasForeignKey(d => d.ColorCode);
});
modelBuilder.Entity<Color>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -1858,11 +1828,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
modelBuilder.Entity<Post>(entity =>
{
entity.HasNoKey();
Expand Down Expand Up @@ -2207,11 +2172,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityWithAnnotation>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -2284,11 +2244,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityWithPropertyAnnotation>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -2349,8 +2304,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasMany(d => d.Posts).WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"BlogPost",
Expand All @@ -2363,11 +2316,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
});
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -2481,8 +2429,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasMany(d => d.Posts).WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"BlogPost",
Expand Down Expand Up @@ -2608,8 +2554,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasMany(d => d.Posts).WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"BlogPost",
Expand All @@ -2622,11 +2566,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
});
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -2756,8 +2695,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasMany(d => d.Posts).WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"BlogPost",
Expand All @@ -2772,11 +2709,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
});
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down Expand Up @@ -2928,8 +2860,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
entity.HasMany(d => d.Posts).WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"PostBlog",
Expand All @@ -2949,11 +2879,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
});
modelBuilder.Entity<Post>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});
OnModelCreatingPartial(modelBuilder);
}
Expand Down

0 comments on commit 03bb7b7

Please sign in to comment.