views:

14

answers:

2
Product product = new Product() {
    Category = category
};

_session.CommitChanges();

vs.

Product product = new Product() {
    Category.ID = category.ID
};

_session.CommitChanges();

What is the difference? Which one to use? Both seem to be valid and get correctly saved in the database.

A: 

I just pass Category.ID.

When the query is executed against the database the only information that is passed to Product is the Category ID, that is, this is the info that will be stored in DB (in each Product row).

Behind the curtains, the engine knows the fields necessary to generate the SQL insert command, in this case, only Category.ID is necessary when saving the Product. That's why no matter what option you choose, the save operation always succeeds.

Leniel Macaferi
A: 

Always use the first version. This will make sure all the relations are in a state they should be , because the keys and stuff are assigned and managed by the RelationshipManager.

psicho