Hi,

I have been working on the entity framework for a short span of time. My question is regarding savechanges() or rather what exactly is hapening in this scenario.

I have 3 tables: Container , subcontainer, innercontainer there is a 1-n relation between tables in the specified order. i have added used the AddObject() to add the objects into the context and i am calleing save changes at the end of it all. But wen i so that i get this following error: "Unable to determine the principal end of the FK relationship. Multiple added entities may have the same primary key". Now i guess what it is telling me is that there are a few objects that i have added with the same PK (EF takes 0 by default). and it shows me on the error on the innercontainer table. My question here is why does it show the error on the innercontiner but not on the subcontainer? (context will contain subcontiner with the same PK of 0).

Relevent Info:
- All PK is autogenerated in DB
- calling save changes after all addition because to increase performance (still a test)

Dani AI

Generated

Short version: the exception means Entity Framework can't decide which side of a relationship is the principal when trying to persist a graph of newly added objects that all carry temporary key values. 's pointer to check the FK configuration is the right direction; the root cause is almost always an ambiguous relationship in the model or in how the objects were attached to the context.

What happens under the covers: when EF prepares to SaveChanges it must pick an insert order and decide which end of each referential constraint is the principal. If navigation properties, FK properties, or fluent mappings don't make that principal/dependent relationship explicit, and several new entities share the same temporary PKs, EF cannot resolve the constraint. The ambiguity typically surfaces at the deepest child in the graph, which is why the error appears on the inner level even when siblings also have temporary keys.

Practical fixes (apply one or more):

  • Make relationships explicit in the CLR model (both nav props and FK properties) and populate the graph before adding to the context. Add the root only so EF can discover the whole graph:
var container = new Container();
var sub = new Subcontainer();
var inner = new Innercontainer();

sub.InnerContainers.Add(inner);
container.Subcontainers.Add(sub);

context.Containers.Add(container); // add root so EF tracks the whole graph
context.SaveChanges();
  • Configure principal/dependent explicitly with the fluent API so EF doesn't have to guess:
modelBuilder.Entity<Subcontainer>()
    .HasRequired(s => s.Container)
    .WithMany(c => c.Subcontainers)
    .HasForeignKey(s => s.ContainerId);
  • Ensure primary-key properties are marked as database-generated (Identity) in the model so EF treats their values as temporary and knows they will be set by the DB.

Quick diagnostics and cautions: inspect ChangeTracker/ObjectStateManager to see temporary key values and entity states, avoid calling AddObject/Add on every entity independently (add the root instead), and check for accidental multiple FKs or missing nav properties that create ambiguity. Explicit mapping and linking via navigation properties will resolve the principal/dependent decision and prevent the error.

Hello necrovore! :)
It sounds like (based on the error) one of your foreign keys has not been configured to a primary key. Check out: https://msdn.microsoft.com/en-us/library/ms179610.aspx for more info on how to do this. :)

Tekkno

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.