编辑帖子操作 - 为什么 respository.save() 保存数据?

发布于 2024-09-06 04:48:41 字数 940 浏览 0 评论 0原文

我有点糊涂了。我无法解释为什么以下内容有效:

    AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues) {
       Dinner dinner = dinnerRepository.GetDinner(id);
       UpdateModel(dinner);
       dinnerRepository.Save();
       return RedirectToAction("Details", new { id = dinner.DinnerID });
    }

该示例取自 Scott Guthrie 的 NerdDinner 演练,其中晚餐Repository.Save() 定义为:

public void Save() {
   db.SubmitChanges();
}

并且dinnerRepository.GetDinner(id) 定义如下:

public Dinner GetDinner(int id) {
   return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
}

即,db NerdDinnerDataContext 是如何工作的“知道”要保存晚餐对象吗?

我对 Linq To SQL 的理解肯定有一个漏洞,但如果我能指出它的话,那就大错特错了。好的,晚餐对象有一个 id,但是什么告诉数据库有需要针对具有该 id 的特定记录提交的更改?

我就是看不到。一定是世界杯...

我只能认为 DataContext 对象 db 保留了对使用 GetDinner 方法调用获取的晚餐对象的引用。但是......这一切感觉有点“神奇”

安德鲁

I am a tad befuddled. I can't reason why the following works:

    AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues) {
       Dinner dinner = dinnerRepository.GetDinner(id);
       UpdateModel(dinner);
       dinnerRepository.Save();
       return RedirectToAction("Details", new { id = dinner.DinnerID });
    }

The sample is taken from Scott Guthrie's NerdDinner walkthrough, where dinnerRepository.Save() is defined as:

public void Save() {
   db.SubmitChanges();
}

And dinnerRepository.GetDinner(id) is defined as follows:

public Dinner GetDinner(int id) {
   return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
}

Ie, how does the the db NerdDinnerDataContext "know" to save the dinner object?

There must be a hole in my understanding of Linq To SQL, but blowed if I can pin point it. OK, so the dinner object has an id, but what tells db that there are changes to submit for that specific record with that Id?

I just can't see it. Must be the World Cup...

I can only think that the DataContext object, db, keeps a reference to the dinner object that was got using the GetDinner method call. But... It all feels a bit 'magical'

Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-09-13 04:48:41

您应该查看 LINQ to SQL 类的生成代码。您将看到类的属性 setter 和 getter 包含更改跟踪,以便下次保存存储库时,生成正确的 SQL 语句来提交对对象进行的任何更改。

You should look at the generated code for the LINQ to SQL classes. You'll see that the property setters and getters for the classes contain change tracking so that the next time the repository is saved, the proper SQL statements are generated to commit any mutations made the the objects.

皓月长歌 2024-09-13 04:48:41

正如您怀疑它是由 ORM 层处理的(Linq2SQL 或实体框架...不记得是哪一个)。 ORM 层跟踪它们所管理的对象的更改并不罕见,并且由于已更改的对象是从 ORM 层检索的(通过 db.Dinners.SingleOrDefault() 调用),因此ORM 正在跟踪该对象的更改,因此当您调用 SubmitChanges 时它就知道它已更改,这都是 ORM 魔力的一部分。

As you suspect it is handled by the ORM layer (which is either Linq2SQL or entity framework...don't recall which). It is not uncommon for ORM layers to track changes to the objects they are managing, and since the object that has changed was retrieved from the ORM layer (via the db.Dinners.SingleOrDefault() call, the ORM is tracking changes to that object, so it knows that it has changed when you call SubmitChanges. It is all part of the magic of ORMs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文