When I run the code below, EF saves PersonAddress with the correct PersonId . I have NOT added PersonAddress entities to the Person object, although I have not done this yet, the records in my database are connected correctly.
My question is: does EF automatically add related objects, even if I did not specify the object to which it belongs? And if so, will this not lead to an undesirable essential relationship?
Update
It seems that the objects were saved correctly for the following reasons:
- When creating a
Person object, the PersonId field is 0 PersonAddress.PersonId also 0 at creation time.
Manually setting Person.PersonId to any value at creation time, and then setting PersonAddress.PersonId to the same Person.PersonId EF value saves the data correctly because they have the same PersonId .
Thus, technically, EF does not automatically add related objects; they are related to each other because they have the same PersonId .
Please check out the code below:
using (var context = new Models.TestEntities()) { var person = context.People.Create(); var postalAddress = context.PersonAddresses.Create(); postalAddress.AddressLine1 = "PostalAddressLine1"; var residentialAddress = context.PersonAddresses.Create(); residentialAddress.AddressLine1 = "ResidentialAddressLine1"; context.People.Add(person); context.PersonAddresses.Add(postalAddress); context.PersonAddresses.Add(residentialAddress); context.SaveChanges(); }
When I add additional Person code to the code, I get the following error: 
Code:
using (var context = new Models.TestEntities()) { var person = context.People.Create(); var person2 = context.People.Create(); var postalAddress = context.PersonAddresses.Create(); postalAddress.AddressLine1 = "PostalAddressLine1"; var residentialAddress = context.PersonAddresses.Create(); residentialAddress.AddressLine1 = "ResidentialAddressLine1"; context.People.Add(person); context.People.Add(person2); context.PersonAddresses.Add(postalAddress); context.PersonAddresses.Add(residentialAddress); context.SaveChanges(); }
Since the indicated error, Entityframework can no longer determine to whom PersonAddress belongs.
I can solve this problem by changing the code as follows:
using (var context = new Models.TestEntities()) { var person = context.People.Create(); var person2 = context.People.Create(); var postalAddress = context.PersonAddresses.Create(); postalAddress.AddressLine1 = "PostalAddressLine1"; var residentialAddress = context.PersonAddresses.Create(); residentialAddress.AddressLine1 = "ResidentialAddressLine1"; context.People.Add(person); context.People.Add(person2); person.PersonAddresses.Add(residentialAddress); person.PersonAddresses.Add(postalAddress); context.SaveChanges(); }
See EDMX below:

See the SQL script used to create two tables:
CREATE TABLE Person ( PersonId INT IDENTITY(1,1) NOT NULL CONSTRAINT [PK_Person] PRIMARY KEY, FirstName VARCHAR(250) ) CREATE TABLE PersonAddress ( PersonAddressId INT IDENTITY(1,1) NOT NULL CONSTRAINT [PK_PersonAddress] PRIMARY KEY, PersonId INT NOT NULL, AddressLine1 VARCHAR(250) ) ALTER TABLE PersonAddress ADD CONSTRAINT [FK_PersonAddress_Person] FOREIGN KEY(PersonId) REFERENCES [Person](PersonId)
Please see the screenshots in the Id columns below: 


Please view the SQL Server Profiler trace below:
Person :

PersonAddress :

PersonAddress :

Please view the inserted records in SQL:
Person :

PersonAddress :

Thanks.