Skip to content

Commit

Permalink
Scaffold entities with no keys
Browse files Browse the repository at this point in the history
part of #1679
  • Loading branch information
ErikEJ authored and smitpatel committed May 7, 2019
1 parent ccdea10 commit 99fb2d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
8 changes: 0 additions & 8 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ Change your target project to the migrations project by using the Package Manage
<data name="PrimaryKeyErrorPropertyNotFound" xml:space="preserve">
<value>Could not scaffold the primary key for '{tableName}'. The following columns in the primary key could not be scaffolded: {columnNames}.</value>
</data>
<data name="MissingPrimaryKey" xml:space="preserve">
<value>Unable to identify the primary key for table '{tableName}'.</value>
</data>
<data name="UnableToGenerateEntityType" xml:space="preserve">
<value>Unable to generate entity type for table '{tableName}'.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void InitializeEntityTypeBuilder(IEntityType entityType)

private void GenerateEntityType(IEntityType entityType, bool useDataAnnotations)
{
GenerateKey(entityType.FindPrimaryKey(), useDataAnnotations);
GenerateKey(entityType.FindPrimaryKey(), entityType, useDataAnnotations);

var annotations = entityType.GetAnnotations().ToList();
RemoveAnnotation(ref annotations, CoreAnnotationNames.ConstructorBinding);
Expand Down Expand Up @@ -417,10 +417,17 @@ private void AppendMultiLineFluentApi(IEntityType entityType, IList<string> line
}
}

private void GenerateKey(IKey key, bool useDataAnnotations)
private void GenerateKey(IKey key, IEntityType entityType, bool useDataAnnotations)
{
if (key == null)
{
var line = new List<string>
{
$".{nameof(EntityTypeBuilder.HasNoKey)}()"
};

AppendMultiLineFluentApi(entityType, line);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,20 @@ protected virtual EntityTypeBuilder VisitTable([NotNull] ModelBuilder modelBuild

VisitColumns(builder, table.Columns);

var keyBuilder = VisitPrimaryKey(builder, table);

if (keyBuilder == null)
if (table.PrimaryKey != null)
{
var errorMessage = DesignStrings.UnableToGenerateEntityType(table.DisplayName());
_reporter.WriteWarning(errorMessage);
var keyBuilder = VisitPrimaryKey(builder, table);

var model = modelBuilder.Model;
model.RemoveEntityType(entityTypeName);
model.Scaffolding().EntityTypeErrors.Add(entityTypeName, errorMessage);
return null;
if (keyBuilder == null)
{
var errorMessage = DesignStrings.UnableToGenerateEntityType(table.DisplayName());
_reporter.WriteWarning(errorMessage);

var model = modelBuilder.Model;
model.RemoveEntityType(entityTypeName);
model.Scaffolding().EntityTypeErrors.Add(entityTypeName, errorMessage);
return null;
}
}

VisitUniqueConstraints(builder, table.UniqueConstraints);
Expand Down Expand Up @@ -489,11 +492,6 @@ protected virtual KeyBuilder VisitPrimaryKey([NotNull] EntityTypeBuilder builder
Check.NotNull(table, nameof(table));

var primaryKey = table.PrimaryKey;
if (primaryKey == null)
{
_reporter.WriteWarning(DesignStrings.MissingPrimaryKey(table.DisplayName()));
return null;
}

var unmappedColumns = primaryKey.Columns
.Where(c => _unmappedColumns.Contains(c))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ public void Creates_entity_types()
},
new DatabaseTable
{
Name = "notScaffoldable"
Name = "noPrimaryKey"
}
}
};
var model = _factory.Create(info, false);
Assert.Collection(
model.GetEntityTypes().OrderBy(t => t.Name).Cast<EntityType>(),
vwtable =>
{
Assert.Equal("noPrimaryKey", vwtable.Relational().TableName);
Assert.Equal(0, vwtable.GetKeys().Count());
},
table =>
{
Assert.Equal("noSchema", table.Relational().TableName);
Expand All @@ -97,7 +102,7 @@ public void Creates_entity_types()
Assert.Equal("public", pgtable.Relational().Schema);
}
);
Assert.NotEmpty(model.Scaffolding().EntityTypeErrors.Values);
Assert.Empty(model.Scaffolding().EntityTypeErrors.Values);
}

[Fact]
Expand Down

0 comments on commit 99fb2d4

Please sign in to comment.