简体   繁体   English

如何修复“每个表只能配置一列'Identity'。在 Ef Core 和 Oracle 中调用 'ValueGeneratedNever'...

[英]How to fix "Only one column per table can be configured as 'Identity'. Call 'ValueGeneratedNever' ... in Ef Core and Oracle

I have an Oracle Database.我有一个 Oracle 数据库。 I tried to Scaffold my data model into my .net core 5 project using EF Core which has been done successfully, but when I try to Get a list from I get this Error:我尝试使用已成功完成的 EF Core 将我的数据 model 搭建到我的 .net 核心 5 项目中,但是当我尝试从中获取列表时,我收到此错误:

Only one column per table can be configured as 'Identity'.每个表只能配置一列作为“身份”。 Call 'ValueGeneratedNever' for properties that should not use 'Identity'.为不应使用“Identity”的属性调用“ValueGeneratedNever”。

The table in database has just one identity column which "ID" and the other column is a simple Number without precision.数据库中的表只有一个标识列“ID”,另一列是一个没有精度的简单数字。 here is my model after reverse engineering:这是我的 model 逆向工程后:

modelBuilder.Entity<CreBranches>(entity =>
        {
            entity.ToTable("CRE_BRANCHES");

            entity.Property(e => e.Id).HasColumnName("ID");

            entity.Property(e => e.BranchCode)
                .HasColumnName("BRANCH_CODE")
                .HasColumnType("NUMBER")
                .ValueGeneratedOnAdd();

            entity.Property(e => e.CreationDate)
                .HasColumnName("CREATION_DATE")
                .HasColumnType("TIMESTAMP(6)")
                .ValueGeneratedOnAdd();}

When I just make a simple select from each model, I get this error:当我从每个 model 制作一个简单的 select 时,我收到此错误:

The properties 'CreBranches.BranchCode', 'CreBranches.Id' are configured to use 'Identity' value generator and are mapped to the same table 'Cre_branches'.属性“CreBranches.BranchCode”、“CreBranches.Id”被配置为使用“Identity”值生成器并映射到同一个表“Cre_branches”。 Only one column per table can be configured as 'Identity'.每个表只能配置一列作为“身份”。 Call 'ValueGeneratedNever' for properties that should not use 'Identity'.为不应使用“Identity”的属性调用“ValueGeneratedNever”。

CRE_BRANCHES: CRE_BRANCHES:

在此处输入图像描述

when I remove ValueGeneratedOnAdd(), it works.当我删除 ValueGeneratedOnAdd() 时,它可以工作。 Does anybody know what to do?有人知道该怎么做吗?

I finally solved the problem.我终于解决了这个问题。

The problem was I did not set default value for my numeric columns in Oracle.问题是我没有为 Oracle 中的数字列设置默认值。 I set the default value of these columns to NULL and the problem been solved using the following command:我将这些列的默认值设置为 NULL 并使用以下命令解决了问题:

ALTER TABLE table_name MODIFY column_name DEFAULT NULL;

So, after scaffolding the database, EF added.HasDefaultValueSql("NULL") to the fluent API:所以,在搭建好数据库之后,EF在流畅的API中添加了.HasDefaultValueSql("NULL"):

entity.Property(e => e.ColumnName)
                .ValueGeneratedOnAdd()
                .HasColumnName("Column_Name")
                .HasDefaultValueSql("NULL");

Thank you all.谢谢你们。

By convention, EF assumes that the column ID is an identity column.按照惯例,EF 假定列 ID 是标识列。 Using.ValueGeneratedOnAdd() on a numeric column also generates an identity column.在数值列上使用.ValueGeneratedOnAdd() 也会生成一个标识列。 A table can only have one identity column.一张表只能有一个标识列。 Please check the EF documentation for column name conventions and the ValueGeneratedOnAdd method for more detail.请查看 EF 文档以了解列名约定和ValueGeneratedOnAdd方法以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM