Entity Framework One-to-Many Insert - Foreign Key Violation

This is the first time I'm using the Entity Framework, and I'm trying to create an object with a collection (and I want all the objects in the collection to be created in the database as well), but I have some foreign key violations.

My sample tables:

table APPOINTMENTS: ID, VAR1, DATE_APPOINTMENT table GUESTS: ID, APPOINTMENT_ID, USER_ID, VAR2, VAR3 

My test code is:

 DomainService aux = new DomainService(); APPOINTMENTS appointment = new APPOINTMENTS(); appointment.VAR1 = "BLA"; appointment.DATE_APPOINTMENT = new DateTime(); //The user with id = 1 is already created in the database appointment.GUESTS.Add(new GUESTS { USER_ID = 1, VAR2 = 1, VAR3 = "F" }); aux.InsertAppointment(appointment); 

In DomainService, I have:

 public void InsertAppointment(APPOINTMENTS appointment) { using (var context = this.ObjectContext) { context.AddToAPPOINTMENTS(appointment); context.SaveChanges(); } } 

But I get this error: {"ORA-02291: integrity violation (FK_GUESTS_APPOINTMENTS) violated - parent key not found"}

What am I doing wrong?

UPDATE: To create an identifier in the database, I use a sequence for each table and a trigger before inserting to get the next value. When I create one object, for example. appointment without guests, it inserts into the database and generates an identifier.

+6
source share
5 answers

The solution to this problem:

"Identification fields that are generated from sequences will not be processed correctly. After saving the objects, the identifier will be returned as 0. I will fix this by manually hacking the SSDL (open your .edmx file in a text editor) with the StoreGeneratedPattern =" Identity "attributes on the Identification fields (lines 6 and 16.) Please note that the designer may break this change after a future modification.

Although I believe this is not entirely necessary, it may also be reasonable to change some type metadata, such as changing the "number" to "int" in your SSDL and "Decimal" from "Int32" in your CSDL, where applicable. Often they are not generated automatically with the required values ​​especially with XE. "

@http: //www.chrisumbel.com/article/oracle_entity_framework_ef

+4
source

For me, the problem was solved simply by opening the .edmx diagram and changing the StoreGeneratedPattern property from None to Identity for each primary key in each table. After saving everything was in order.

I am using VS 2012, Entity Framework 5 (not yet supported 6), Oracle 11.2, latest ODP.NET 12, .Net 4.5

+1
source

In the case of the first approach of the EF code, if this error occurred

(ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found)

In my case, there are 2 tables that have Identity columns. So I just added

 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

for my model class just above the column, which is the identity column in the database, and it solved my problem :)

Hope this help :)

+1
source

I do not see where you are setting your primary key (property of the ID of the destination class). Do you use a key generator on the database side? If not, this should be a problem.

0
source

Insert a record with an external key value that is not found in the parent table referenced by the constraint.

-1
source

Source: https://habr.com/ru/post/898369/


All Articles