开发者

What is the preferred way to set foreign relationships for an entity?

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

_session.CommitChanges();

vs.

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

_sessio开发者_开发知识库n.CommitChanges();

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜