0

I am teaching myself about the entity framework using a tutorial found at http://msdn.microsoft.com/en-us/data/jj591506.

using (var db = new DatabaseEntities())
{
            // Section 1
            var section = new Section
            {
                SectionID = 1,
                SectionLetter = "d"
            };
            db.Sections.Add(section);
            db.SaveChanges();

            // Section 2

            var query = from b in db.Sections 
                        orderby b.SectionLetter
                        select b;

            foreach (var item in query)
            {
                this.Title = item.SectionLetter;
            }
}

When I use section 1 and section 2 of the code above together they work well. However, when I run just Section 2 the title does not change which means it does not even return a single item and thus does not enter the foreach loop. I can't see why it would not enter the loop.

I realise there is probably an obvious answer to this question but I thought I should ask on stackoverflow rather than ask a colleague so others with the same question could find an answer.

I suspect there might be a clue in this question: System.Data.Entity not working as expected for MVC Entity Framework. Unfortunately I can't seem to work it out.

4
  • What have you tried? Commented Dec 30, 2012 at 10:45
  • This seems to be too simple, I guess you've not even tried to debug you code ... Commented Dec 30, 2012 at 10:55
  • If you have done debugging, what was the title property like after you left the using block? Was it emty? Is the content not just rendered on the UI? Are you blocking the UI somehow? There are tons of possibilities what can go wrog. The code you posted seems to be ok, so you have to provide more info about the specific problem. Commented Dec 30, 2012 at 11:10
  • The title property is unchanged which means it does not even return a single item and thus does not enter the foreach loop. I can't see why it would not enter the loop. The rest of the code is thus not relevant. The question is why it does not find any records unless they were just added. Thanks for trying to be helpful this time :-) Commented Dec 30, 2012 at 11:14

2 Answers 2

1

It looks like the line var db = new DatabaseEntities() is connecting to a database that doesn't have any rows. Check the Connection property of db to make sure it is on the correct server/instance/file.

You should be able to see the contents of the table if you perform a "Quick Watch" on query.ToArray().

Sign up to request clarification or add additional context in comments.

Comments

1

are you sure that if you only run section 2:

var query = from b in db.Sections 
            orderby b.SectionLetter
            select b;

that the query variable actually yields results? have you debugged it to the point where you see what is inside query?

  var query = (from b in db.Sections 
              orderby b.SectionLetter
              select b).ToList();

try this, and put a breakpoint right after it. Then see how many elements are in the query variable. If there are 0, its logical that your foreach isnt going to do alot.

Have you checked your actual database to see wether or not there are any records in there ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.