EFCore数据库第一个脚手架错误

时间:2018-01-27 01:51:29

标签: postgresql entity-framework ef-database-first

我正在尝试将现有的PostgreSQL数据库转换为具有Entity Framework Core 2.0.1的实体,但是我在脚手架时遇到错误。数据库是通过运行以下脚本创建的: https://stackblitz.com/edit/angular-xfrezb

然后我在VS2017中运行以下命令:

System.ArgumentException: The collection argument 'propertyNames' must contain at least one element.
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty[T](IReadOnlyList`1 value, String parameterName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.HasIndex(String[] propertyNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitIndex(EntityTypeBuilder builder, DatabaseIndex index)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitIndexes(EntityTypeBuilder builder, ICollection`1 indexes)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTable(ModelBuilder modelBuilder, DatabaseTable table)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTables(ModelBuilder modelBuilder, ICollection`1 tables)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(ModelBuilder modelBuilder, DatabaseModel databaseModel)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.CreateFromDatabaseModel(DatabaseModel databaseModel, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The collection argument 'propertyNames' must contain at least one element.

该操作运行良好一点(有一些警告,见下文),但它失败并显示以下错误消息:

Sequence with datatype integer which isn't the expected bigint.
Sequence with datatype integer which isn't the expected bigint.
Sequence with datatype integer which isn't the expected bigint.
The column 'tiger.street_type_lookup.is_hw' would normally be mapped to a non-nullable bool property, but it has a default constraint. Such a column is mapped to a nullable bool property to allow a difference between setting the property to false and invoking the default constraint. See https://go.microsoft.com/fwlink/?linkid=851278 for details.

有人知道这里的问题是什么吗?

我也收到以下两种类型的警告。这是次要的,但我也非常感谢帮助理解和解决它们。

F:\Downloads\Downloads\500 Comics CCC English\Jack, Byrd - Art #01.cbr

4 个答案:

答案 0 :(得分:1)

您是否有任何带表达式的索引作为参数? 类似的东西:

UserControl

表达式可以在pg_indexes表中插入为已损坏。

您可以通过

查看
DataContext
祝你好运!

答案 1 :(得分:0)

关于序列警告,序列返回类型和主键列Id之间可能不匹配。您可以使用BIGSERIAL,即BIGINT +自动增量,这样就可以避免序列手动输入。作为foreing键列类型使用BIGINT。

关于is_hw列,它有一个可能导致可以为空的值的约束,另一方面,同一列可能被设置为NOT NULL。

这会帮助你https://github.com/npgsql/npgsql/issues/892吗?

答案 2 :(得分:0)

我不确定这篇文章是否仍然有效;但是,在使用Entity Framework时我遇到了同样的错误,事实证明我的sql帐户需要对数据库的额外访问权限。我给它db.owner访问权限,因为这只是一个本地测试项目,它工作。

答案 3 :(得分:0)

此错误与 @tim-potter 回答说这是索引的错误用法完全一样,导致运行时为 System.ArgumentException: The collection argument 'propertyNames' must contain at least one element.

要解决这个问题: 在模型类中,找到这个属性 [Index(Name = "PropertyName", IsUnique = true/false)],当你找到那个属性时,确保字符串 Name 而不是:

Name = "PropertyName" 使用 nameof(PropertyName)

我认为索引属性中的 Name 属性不起作用的原因是实体框架中的错误。