Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 382401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:10:45+00:00 2026-05-12T15:10:45+00:00

I am rewriting my application to use the entity framework. What I am confused

  • 0

I am rewriting my application to use the entity framework. What I am confused about is the code I am writing looks like it is making unnecessary tripts the the sql server. For example, I have a question answer site similar to SO. When I add an answer to a question — here is the code I use:

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

In the code above there are 2 database calls right? If so, why can’t I add an answer to a question directly? such as

var ans = answer.Createanswer (0, "title here", "desc here", questionID)
context.SaveChanges();

Is there a way to minimize all the database calls?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:10:45+00:00Added an answer on May 12, 2026 at 3:10 pm

    As explained by AlexJ one of the EF designers http://blogs.msdn.com/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx

    Also this all falls into the realm on “optimisation” which in not often as simple as it seems

    Using the the simple approach, SQL will do a read operation to load the FK (question) and cache the result, and then on a seperate command an insert operation which should be using the cached FK result

    Using the attached FK method still results in the server doing a read operation for the FK, it just means one less round trip to the SQL Server. So the question becomes – over time is a round trip more expensive than the increased code complexity?

    If the application and SQL Server are on the same machine this overhead is very small

    Also, if the FK is a clustered index on a large or wide table the IO overhead can be significantly more than if it is a seperate standard index on just the FK value – assuming that the query optimiser is working correctly 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an MVC3 application that will need to make use of URL rewriting
I'm in the midst of rewriting a database application and using Entity Framework to
OK, so we're writing our MFC application to make use of the built-in localization
I'm rewriting an LOB application whose architecture is like this: Silverlight && Windows Mobile
I am writing a Django application, and I often use the Python backtick operator
I`m rewriting an application that use MS Access as the database. I was able
Im rewriting application from .NET to PHP. I need to create class like this:
I am currently working on rewriting an application to use Data Mappers that completely
I have RewriteMap configured to use an external application for rewriting the inbound URL.
I'm working with Entity Framework 4.1 in an MVC3 Web Application. I am tasked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.