ORA 02291 integrity constraint violated-parent key not found

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
Judge
Posts: 4
Joined: Wed 03 Jun 2020 08:05

ORA 02291 integrity constraint violated-parent key not found

Post by Judge » Wed 03 Jun 2020 08:25

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

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Fri 05 Jun 2020 21:14

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.

Judge
Posts: 4
Joined: Wed 03 Jun 2020 08:05

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

Post by Judge » Mon 08 Jun 2020 07:13

Hy,

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

Thank you very much for your help.

Best regards Roman

Post Reply