Page 1 of 1

ORA 02291 integrity constraint violated-parent key not found

Posted: Wed 03 Jun 2020 08:25
by Judge
Hello,

im am using Visual studio 2019, .NET Core 3.1 and devart.data.oracle.efcore (9.11.980).

I get the Error ORA-02291, when i am trying to save values in two Tables with dependencies.

For Example:

The Properties ID from TABLEA and TABLEB are generated in the Oracle-Database.

TABLEB.ID_TABLEA is linked to TABLEA.ID

using (Data.Model context = new Data.Model())
{
TABLEA newRowA = new TABLEA();
newRowA.TEXT = "hy";
context.TABLEA.Add(newRowA);

TABLEB newRowB = new TABLEB();
newRowB.ID_TABLEA = newRowA.ID;
context.TABLEB.Add(newRowB);

context.SaveChanges() > ERROR 02291
}

The ID-Properties of both Tables ar set to Value Generated "OnAdd".

When i perform context.SaveChanges() after adding TABLEA the ID is generated and i can save TABLEB.
But when one insert fails, then nothing should be in the Database.
That is not an Option in this case.

Thanks for your help.

Roman

Re: ORA 02291 integrity constraint violated-parent key not found

Posted: Fri 05 Jun 2020 21:14
by Shalex
Judge wrote: Wed 03 Jun 2020 08:25I get the Error ORA-02291, when i am trying to save values in two Tables with dependencies.
IDs do not exist in .NET objects until you call SaveChanges(). You should use navigation property instead of assigning non-existing ID:
newRowB.ID_TABLEA = newRowA.ID;
->
newRowB.ToTableANavigationProperty = newRowA;
Judge wrote: Wed 03 Jun 2020 08:25When i perform context.SaveChanges() after adding TABLEA the ID is generated and i can save TABLEB.
But when one insert fails, then nothing should be in the Database.
That is not an Option in this case.
JIC: you can use transactions, or wrap your code with the global transaction.

Re: ORA 02291 integrity constraint violated-parent key not found

Posted: Mon 08 Jun 2020 07:13
by Judge
Hy,

I began using navigation properties as you suggested. That works fine.

Thank you very much for your help.

Best regards Roman