Page 1 of 1

Problem with Entity Inheritance

Posted: Wed 27 Feb 2013 20:29
by SchneiderM
Hello,
I'm having a problem with EF5 DotConnect 7.4 and entity inheritance.
I've the following setup:

First Entity:

Code: Select all

    public class Parent
    {
        public int Id { get; set; }
        public string Name1 { get; set; } 
    }


Second Entity:

Code: Select all

    public class Child : Parent
    {
        public string Name2 { get; set; }
    }
DatabaseContext:

Code: Select all

    public class DataContext : DbContext 
    {
        public DataContext() : base("name=" + Environment.MachineName)
        {
        }

        public DbSet<Parent> Parents { get; set; }

        public DbSet<Child> Childs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>().ToTable("TBL_PARENT");
            modelBuilder.Entity<Child>().ToTable("TBL_Child");
            base.OnModelCreating(modelBuilder);
        }
    }
Migration.Configuration:

Code: Select all

    internal sealed class Configuration : DbMigrationsConfiguration<EntityFramework.DataContext>
    {
        public Configuration()
        {
            AutomaticMigrationDataLossAllowed = true;
            AutomaticMigrationsEnabled = true;

            SetSqlGenerator(OracleConnectionInfo.InvariantName, new OracleEntityMigrationSqlGenerator());

            var config = OracleEntityProviderConfig.Instance;
            config.Workarounds.IgnoreSchemaName = true;
            config.Workarounds.ColumnTypeCasingConventionCompatibility = true;
            config.CodeFirstOptions.TruncateLongDefaultNames = true;
            config.DatabaseScript.Schema.DeleteDatabaseBehaviour = DeleteDatabaseBehaviour.AllSchemaObjects;
        }
   ...
}
When I run Migration to generate the initial database, I get this error:
Devart.Data.Oracle.OracleException (0x80004005): ORA-00955: name is already used by an existing object
at Devart.Data.Oracle.ay.c(Int32 A_0)
at Devart.Data.Oracle.cm.a(Int32 A_0, b7 A_1)
at Devart.Data.Oracle.OracleCommand.InternalExecute(CommandBehavior behavior, IDisposable disposable, Int32 startRecord, Int32 maxRecords, Boolean nonQuery)
at Devart.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior, Boolean nonQuery)
at Devart.Data.Oracle.OracleCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ORA-00955: name is already used by an existing object
The script to create the database:
CREATE TABLE TBL_PARENT (
"Id" NUMBER(10) NOT NULL,
"Name1" NCLOB NULL,
PRIMARY KEY ("Id")
)
/

CREATE SEQUENCE TBL_PARENT_SEQ
/

CREATE OR REPLACE TRIGGER TBL_PARENT_INS_TRG
BEFORE INSERT ON TBL_PARENT
FOR EACH ROW
BEGIN
SELECT TBL_PARENT_SEQ.NEXTVAL INTO :NEW."Id" FROM DUAL;
END;
/

CREATE TABLE "TBL_Child" (
"Id" NUMBER(10) NOT NULL,
"Name2" NCLOB NULL,
PRIMARY KEY ("Id")
)
/

CREATE INDEX "IX_TBL_Child_Id" ON "TBL_Child" ("Id")
/

ALTER TABLE "TBL_Child"
ADD CONSTRAINT "FK_TBL_Child_TBL_PARENT_Id" FOREIGN KEY ("Id") REFERENCES TBL_PARENT ("Id")
/

CREATE TABLE "__MigrationHistory" (
"MigrationId" NVARCHAR2(255) NOT NULL,
"Model" BLOB NOT NULL,
"ProductVersion" NVARCHAR2(32) NOT NULL,
PRIMARY KEY ("MigrationId")
)
/

DECLARE
BINARY_VALUE BLOB;
BEGIN
DBMS_LOB.CREATETEMPORARY(BINARY_VALUE, TRUE);
DBMS_LOB.APPEND(BINARY_VALUE, TO_BLOB(CAST('1F8B0800000000000400CD58CD8EDB3610BE17E83B083AB587585E2787762127F07AD7C5A2F17A6139B91AB434F612A14881A4B6EB67CBA18FD457E85092F547F9379BB4174322E7F7E37C9C91FFF9FAB7FFE12566CE334845051FBA57BDBEEB000F4544F966E8A67AFDE637F7C3FB9F7FF2EFA2F8C5F9BC937B6BE45093ABA1FBA47572ED792A7C8298A85E4C43299458EB5E28628F44C21BF4FBBF7B57571EA009176D398E3F4FB9A631642FF83A163C8444A7844D45044C15EBB81364569D0712834A480843F70E35F5762271E52F21BFB8CE8851826104C0D6AE93BCBBFEA420D052F04D90104D095B6C13C0FD35610A8AA0AF9377A7C6DD1F98B83DC2B9D0684EF08BF276CB8C30A73C03135696D7D07D2412B8AECBA0D49FB06D2CE0D2A3140948BD9DC3BAD0BC8F5CC76BEA796DC552ADA6639CE313D76F07AEF3903246560C4A8C10C4400B097F00074934448F446B90DCE84216BCE5B5E5C3FC5EEDDCE0696035B9CE84BE40F411F8463F95AEA6E465B7828FAEF389532C3E54D232857A68F97BDDABEF55401E8477FC4419E67C4314140161A9F43A41EF4863F09FA681CCD084E2311411DD124DCC1ABCB443CFE503D08DA252AE53996F66DE1945E9AFA2A0977370C7556F0F59FD29491204A846DE62C50972E68EDF04E79327CE6D78A1EAE050196DE909CB966CA0B58BAE31D209954A1BFC56C41CD9388A2DB113D0DD796A82DC666A85F94EDE3C17FC53E679B6FEA5759315E7F26BDB5805248A6E6214C9D28432A4EE0BA4500D42C288ECB802C682A531DF778D1CD22EC85D37502CD9367CAF157B1B29CF82EA1C2CDB10E6543F1FC0C5CDC7E51EDDEF87E1C0C670F03D30DCED57D57B88F86D91D27B7901B488EE17A43BDEBA2D16E622AE83E03CD32863203C13A97B46A03793246450ED4E09A76B507A21BE00F6A2D97CD46AFE1734664FA988FD3FBB333577F4D13E6C35EF331A330F9958BD5293DDC3A01F06D405380C5E0187915222A4D960589F3696460E29A5B44452E9E5448A789917D2722196FBC0BAE3913317AC2A3AA7A36D4F53A669C26888E1E0C46E655A19A9DCD4EDD4569BA6FABD9E6D0D0F098C5F1CA4AB7CEC13A53CA40961CDE8ED9BEC94A337609706DB3BB7900037C56FA578B9B3D266AB028FA5EE7BB5D33F3EBAD933C82903DC81F92DBF3D876EB41278C079E51A3C1E47F3BB8745475D34EDD7EAC07251DBDBE7A5D86E39A92152793A8F124ECD4611CE3770AA9355F6E8649745278D3A4706EBEA6A8270D9946D37552CC8DA57333241D14D65C27C4373081BA558CADCF3B5D8B1A115D14EA4753F4E419308EB7424355D9350E376084A651F409F094BCDEC15AF20BAE7B35427A9C694215EB1C687A161D621FFD9A74433667F969837F51A29609814538019BF49B3632AE29ED8FD619F0943D9A2F99AB3D4A6096FB6A5A507C14F3454C057DE340B881386C6D48C07E419F6C7761CC32662FE2D251B1C875561A3D2377FE278E65F9CF7FF02D9642A51F7110000' AS LONG RAW)));
INSERT INTO "__MigrationHistory" ("MigrationId", "Model", "ProductVersion") VALUES ('201302271937440_V1', BINARY_VALUE, '5.0.0.net45');
END;
/
When I run the script in the SQLDeveloper I get a more detailed error. Her the snipped with the error:
CREATE INDEX "IX_TBL_Child_Id" ON "TBL_Child" ("Id")
Error by Line:26 Row:47
Error Report:
SQL-Error: ORA-01408: such column list already indexed
01408. 00000 - "such column list already indexed"
The general problem, I guess, is the Foreign Key Convention.
By 'normal' Tables this convention is rather us full and generates and Index for each foreign key reference, but in case of an inheritance there is already an index created by the Primary Key.

Oracle is now complaining about the creation of the duplicate key. MS SQL is in this case more robust.

Any suggestions to fix this problem?

Sorry for the long post :(

Kind Regards
Markus

Re: Problem with Entity Inheritance

Posted: Thu 28 Feb 2013 13:34
by Shalex
Thank you for your report. We are investigating the problem.

Re: Problem with Entity Inheritance

Posted: Thu 07 Mar 2013 13:08
by Shalex
The bug with creating and removing primary key and index, which are built for the same set of columns, in Code-First Migrations is fixed. Starting from the next public build of dotConnect for Oracle, the behaviour of Code-First Migrations will be changed in the following way:
1. If a table has a primary key, then an additional explicit index is not created if it should be created for the same columns used by the primary key.
2. If an index of a table is deleted before dropping the table, we do not generate DROP INDEX for automatically named indices with non-defined user Oracle-specific parameters.

We will post here when the corresponding build is available for download.

Re: Problem with Entity Inheritance

Posted: Thu 14 Mar 2013 16:04
by Shalex
New build of dotConnect for Oracle 7.6.202 is available!
It can be downloaded from http://www.devart.com/dotconnect/oracle/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=1&t=26199.