为实体设置外部关系的首选方式是什么?
Product product = new Product() {
Category = category
};
_session.CommitChanges();
vs
Product product = new Product() {
Category.ID = category.ID
};
_session.CommitChanges();
有什么区别?使用哪一个?两者似乎都是有效的并且正确保存在数据库中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
始终使用第一个版本。这将确保所有关系都处于应有的状态,因为键和内容是由关系管理器分配和管理的。
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.
我只是传递 Category.ID。
当对数据库执行查询时,传递给产品的唯一信息是类别 ID,也就是说,这是将存储在数据库中的信息(在每个产品行中)。
在幕后,引擎知道生成 SQL 插入命令所需的字段,在这种情况下,保存产品时仅需要 Category.ID。这就是为什么无论您选择什么选项,保存操作总是会成功。
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.