【发布时间】:2009-10-02 22:43:00
【问题描述】:
我正在重写我的应用程序以使用实体框架。我感到困惑的是我正在编写的代码看起来像是对 sql 服务器造成了不必要的麻烦。例如,我有一个类似于 SO 的问答网站。当我添加一个问题的答案时——这是我使用的代码:
var qu = context.question.where(c => c.questionID == 11).First(); //Database call here
var answer = new answer();
answer.title = "title here";
answer.desc = "desc here";
answer.question = qu;
context.SaveChanges(); //Database call here
在上面的代码中有 2 个数据库调用对吗?如果是这样,为什么我不能直接添加问题的答案?比如
var ans = answer.Createanswer (0, "title here", "desc here", questionID)
context.SaveChanges();
有没有办法最小化所有的数据库调用?
【问题讨论】:
标签: c# asp.net-mvc entity-framework linq-to-entities