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.
source share