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

时间:2013-08-07 19:04:36

标签: c# oracle entity-framework

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

{
    /// <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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

问题与EF内部机制有关,而不是高估某些内部名称......

因此,Oracle中不存在Module_Id,因为表字段是MODEL_ID。我还在努力...