Oracle上的实体框架外键问题——代码优先

本文关键字:问题 代码 实体 框架 Oracle | 更新日期: 2023-09-27 18:26:43

我首先使用代码进行了以下映射:

{
    /// <summary>
    /// Entity Framework Dc.Dc database table object representation
    /// </summary>
    [Table("DCDC", Schema = "MZMESDB")]
    public class EfDcDc
    {
        /// <summary>
        /// Element ID
        /// </summary>
        [Column("ID")]
        public int Id { get; set; }
        /// <summary>
        /// Name of DC
        /// </summary>
        [Column("NAME")]
        public string Name { get; set; }
        /// <summary>
        /// DC Description
        /// </summary>
        [Column("DESCRIPTION")]
        public string Description { get; set; }
        /// <summary>
        /// Foreign Key
        /// </summary>
        public virtual EfSystemDataModule Module { get; set; }
        /// <summary>
        /// Name of module
        /// </summary>
        [Column("ENABLED")]
        public string Enabled { get; set; }
    }
}

{
    /// <summary>
    /// Entity Framework SystemData.Module database table object representation
    /// </summary>
    [Table("SYSTEMDATAMODULE", Schema = "MZMESDB")]
    public class EfSystemDataModule
    {
        /// <summary>
        /// Element ID
        /// </summary>
        [Column("ID")]
        public int Id { get; set; }
        /// <summary>
        /// Name of module
        /// </summary>
        [Column("NAME")]
        public string Name { get; set; }
        /// <summary>
        /// If the module is installed. Char because Oracle does not support boolean
        /// </summary>
        [Column("INSTALLED")]
        public char Installed { get; set; }
        /// <summary>
        /// If the module is enabled. Char because Oracle does not support boolean
        /// </summary>
        [Column("ENABLED")]
        public char Enabled { get; set; }
    }
}

Oracle表有一个外键:

CREATE TABLE "MZMESDB"."DCDC" ( 
                                "ID" INTEGER NOT NULL , 
                            "NAME" VARCHAR2(64) NOT NULL , 
                            "DESCRIPTION" VARCHAR(256),
                            "MODULE_ID" INTEGER NOT NULL,
                            "MODULE_NAME" VARCHAR(64) NOT NULL,
                            "ENABLED" CHAR NOT NULL, 
                            PRIMARY KEY ("ID") VALIDATE,
                            FOREIGN KEY (MODULE_ID, MODULE_NAME) REFERENCES SYSTEMDATAMODULE(ID, NAME)

编译没有问题,但在我发布时

MzDbContext dbContext = new MzDbContext();
            EfDcDc configuredDC = new EfDcDc();
            try
            {
                configuredDC = dbContext.efDcDc.FirstOrDefault(item => item.Name == "COMMON_NAME");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error reading from database. Message: " + e.Message.ToString());
                return false;
            }

我从Oracle EF驱动程序中得到以下错误:

ORA-00904: '"Extent1'"."'"Module_Id'":Invalid identifier.

我只是想检查数据库中是否存在DcDc元素,以便以后处理它的数据。我做错了什么?

Rds

Oracle上的实体框架外键问题——代码优先

这通常来自无效的列名。在Oracle中,列名的所有内容都是大写的。混合大小写Module_Id来自哪里?我只看到你代码中使用的ID。

该问题与EF内部机制有关,该机制没有在某些内部名称的大写。。。

因此,Module_Id在Oracle中不存在,因为表字段是MODEL_Id。我仍在努力。。。