0

I am following this strategy to create relations without navigation property EF Code First foreign key without navigation property (first answer)

Now what I wonder is that: If I have 2 objects with relationship

Parent { int Id, int ChildId } 
Child { int Id, int ParentId } 

As right now both Id is not yet created and there is no navigation property to link them. How can I add both parent and child in one dbContext.SaveChanges()

qkhanhpro
  • 3,174
  • 1
  • 23
  • 36
  • First, why wouldn't you establish the FK relationship? Second, there's no need to have the relationship in both the parent and child. The child should have the parent's Id as a FK. There shouldn't be a ChildId in the parent. If it's a 1:1 relationship you can either merge to a single entity or use the Parent.Id as the Child's PK. – pjs Mar 23 '22 at 11:26
  • Without a navigation property it's impossible if the primary keys are db-generated. – Gert Arnold Mar 23 '22 at 12:04

0 Answers0