Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework One-To-Many Insert - Foreign Key violation

I'm using Entity Framework for the first time and I'm trying to create a object with a collection (and I want all the objects in the collection to be created in database as well) but I'm having some foreign keys violations.

My sample tables:

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

My test code:

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);

At DomainService I have:

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

But I'm getting this error: {"ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found"}

What am I doing wrong?

UPDATE: To create the ID's in the database, I am using a sequence for each table and a trigger before insert to get the next value. When I create a single object, e.g. a appointment without guests, it inserts in the database and it generates the id.

like image 489
Canastro Avatar asked Sep 30 '11 11:09

Canastro


People also ask

Does the FOREIGN KEY go on the one or many side?

Whichever one is not the primary key is the foreign key. In one-to-many relationships, the FK goes on the "many" side. It can't go on the "one" side because that's where the PK goes and the definition of a primary key includes disallowing duplicates.

Can an entity have multiple foreign keys?

A table can have multiple foreign keys based on the requirement.

How many foreign keys can an entity have?

A table with a foreign key reference to itself is still limited to 253 foreign key references. Greater than 253 foreign key references are not currently available for columnstore indexes, memory-optimized tables, Stretch Database, or partitioned foreign key tables. Stretch Database is deprecated in SQL Server 2022 (16.

Can a FOREIGN KEY appear more than once?

Any column, or set of columns in a table can be defined as a Foreign Key, as long as it is also the primary key or unique key of some table in the database. And there can be any number of FKs defined in a table, they can even overlap.


3 Answers

The solution to this problem:

"The ID fields that are generated from sequences won't be handled correctly. After saving the entities the ID's will be returned as 0. I'll fix this by manually hacking the SSDL (open your .edmx file in a text editor) with StoreGeneratedPattern="Identity" attributes on the ID fields (lines 6 and 16). Note that designer may rip that change out upon future modification.

While I suppose it's not absolutely necessary it might also be prudent to modify some type metadata such as changing "number"s to "int"s in your SSDL and "Decimal"s to "Int32"s in your CSDL where applicable. Frequently these don't auto-generate with the desired values especially with XE."

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

like image 98
Canastro Avatar answered Oct 01 '22 19:10

Canastro


As for me, the problem was solved simply by opening diagram .edmx and changing property StoreGeneratedPattern from None to Identity for each Primary Key in each table. After saving everything was fine.

I'm using VS 2012, Entity Framework 5 (6 is not supported yet), Oracle 11.2, last ODP.NET 12, .Net 4.5

like image 36
Alexey Shumeyko Avatar answered Oct 01 '22 19:10

Alexey Shumeyko


In case of EF code first approach, if this error come

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

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

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

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

Hope this help :)

like image 44
Spandey Avatar answered Oct 01 '22 19:10

Spandey