Reputation: 67
I have a newbie question about EF4.
Let's say I have in my EDMX a class City
and another one called State
.
City goes as following:
Id
Description
State (obj)
And here goes State:
Id
Description
Cities (obj collection)
My question is very simple.
When I create a city and set its state city.State = stateX
does the EF automaticly adds my city to state.Cities
? Or do I have to set it manually?
Thanks.
Upvotes: 3
Views: 308
Reputation: 7448
Well, that depends.
First of all, are you using a model first, a database first or a code first approach?
In model-first you generate your model through EDMX and then let it create the DB for you. As long as there are relationship between the two entities, the foreign key will be generated and you'll have the expected behavior.
In database first is almost the same, but you first create the database (with the foreign keys and all the other stuff) and then derive the EDMX model from it. Again, if the database is structured correctly, no problems.
In Code First, you create your POCO objects like you described (sort of anyway) and then configure the relationships through the fluent interface. If you configure it correctly, the generated database will have the correct foreign keys and again, expected scenario.
Upvotes: 2