diff --git a/src/EFCore.Design/Properties/DesignStrings.Designer.cs b/src/EFCore.Design/Properties/DesignStrings.Designer.cs
index 140bb017da8..d9abae2e16a 100644
--- a/src/EFCore.Design/Properties/DesignStrings.Designer.cs
+++ b/src/EFCore.Design/Properties/DesignStrings.Designer.cs
@@ -464,14 +464,6 @@ public static string PrimaryKeyErrorPropertyNotFound([CanBeNull] object tableNam
GetString("PrimaryKeyErrorPropertyNotFound", nameof(tableName), nameof(columnNames)),
tableName, columnNames);
- ///
- /// Unable to identify the primary key for table '{tableName}'.
- ///
- public static string MissingPrimaryKey([CanBeNull] object tableName)
- => string.Format(
- GetString("MissingPrimaryKey", nameof(tableName)),
- tableName);
-
///
/// Unable to generate entity type for table '{tableName}'.
///
diff --git a/src/EFCore.Design/Properties/DesignStrings.resx b/src/EFCore.Design/Properties/DesignStrings.resx
index cc751c89ed4..f227bfacfd2 100644
--- a/src/EFCore.Design/Properties/DesignStrings.resx
+++ b/src/EFCore.Design/Properties/DesignStrings.resx
@@ -297,9 +297,6 @@ Change your target project to the migrations project by using the Package Manage
Could not scaffold the primary key for '{tableName}'. The following columns in the primary key could not be scaffolded: {columnNames}.
-
- Unable to identify the primary key for table '{tableName}'.
-
Unable to generate entity type for table '{tableName}'.
diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
index 3d3e096c301..eb0fccd054a 100644
--- a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
+++ b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
@@ -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);
@@ -417,10 +417,17 @@ private void AppendMultiLineFluentApi(IEntityType entityType, IList line
}
}
- private void GenerateKey(IKey key, bool useDataAnnotations)
+ private void GenerateKey(IKey key, IEntityType entityType, bool useDataAnnotations)
{
if (key == null)
{
+ var line = new List
+ {
+ $".{nameof(EntityTypeBuilder.HasNoKey)}()"
+ };
+
+ AppendMultiLineFluentApi(entityType, line);
+
return;
}
diff --git a/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs b/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs
index 31ba385d4c0..64b4d852cc1 100644
--- a/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs
+++ b/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs
@@ -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);
@@ -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))
diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs
index de10f66630b..cc2ff5848fd 100644
--- a/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs
+++ b/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs
@@ -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(),
+ vwtable =>
+ {
+ Assert.Equal("noPrimaryKey", vwtable.Relational().TableName);
+ Assert.Equal(0, vwtable.GetKeys().Count());
+ },
table =>
{
Assert.Equal("noSchema", table.Relational().TableName);
@@ -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]