CORE EF生成ORACLE数据库模型报错问题记录

需求:最近在新开发一套在LINUX运行的API接口,需要用到net core api框架以及oracle数据库,首先需要解决的就是连接数据库问题,由于是DBFirst 加上之前很多老表不规范,导致了core EF生成模型报错。正在解决。

net core版本: 2.2

依赖库:

Microsoft.EntityFrameworkCore 2.2.6
Oracle.EntityFrameworkCore  2.2.6
Microsoft.EntityFrameworkCore.Tools 2.2.6
Microsoft.VisualStudio.Web.CodeGeneration.Design 2.2.4

在VS2019控制台通过以下语句生成数据库模型

 Scaffold-DbContext "DATA SOURCE=地址:1521/zmbx;PASSWORD=密码;PERSIST SECURITY INFO=True;USER ID=XXX;Validate Connection = true;" Oracle.EntityFrameworkCore -OutputDir Models -Project DBLib -Force

采用-Tables USERMOBILE 去生成表模型,可以生成 。 但是多个表生成就会有问题。根据提示信息表示可能是存在主键为空。有没有办法跳过为空的继续生成?

错误如下 

The column 'xxx.xxx.ISCREATEPLAN' 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.
Unable to identify the primary key for table 'xxx.xxx'. Unable to generate entity type for table 'xxx.xxx'. System.InvalidOperationException: The property list {'UserId', 'UserId'} cannot be used, because it contains a duplicate - 'UserId'. at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddKey(IReadOnlyList`1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasKeyInternal(IReadOnlyList`1 properties, Nullable`1 configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 propertyNames, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.HasKey(String[] propertyNames) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitPrimaryKey(EntityTypeBuilder builder, DatabaseTable table) 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.Create(DatabaseModel databaseModel, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String namespace, String language, String contextDir, String contextName, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions) at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, 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 outputDbContextDir, 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 property list {'UserId', 'UserId'} cannot be used, because it contains a duplicate - 'UserId'.
原文地址:https://www.cnblogs.com/souphm/p/12060102.html