Wiktor
Wiktor

Reputation: 856

What is happening while adding object with navigation properies to Db?

I've got class like that:

class City
{
    long CityId {get;set;} 
    string Name {get;set;} 
    List<House> Houses {get;set;} 

}

with filled List<House> with some Houses objects and I want to add City to db with db.Cities.Add(someCity).

Is Houses also will be added automatically in that moment?

What I need to do to add also Houses automatically, assuming that my Navigation Properties are set up properly?

Upvotes: 0

Views: 34

Answers (1)

jorgonor
jorgonor

Reputation: 1719

Yes, they will, but take into account that if you do it this way, new House entries will be created in the table. If you want to associate existing houses you will need to add an entity retrieved from database.

Upvotes: 1

Related Questions