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

  • SEARCH
  • Home
  • 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 6112703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:46:31+00:00 2026-05-23T14:46:31+00:00

I don’t understand why code first does not add a new item to the

  • 0

I don’t understand why code first does not add a new item to the collection until after calling savechanges. I installed EF4.1 from NuGet (4.1.10331.0). I created the following example:

public class TinyItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class TinyContext : DbContext
{
    public virtual DbSet<TinyItem> Items { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var ctx1 = new TinyContext())
        {
            ListItems(ctx1, "Start");

            ctx1.Items.Add(new TinyItem { Name = "Test1" });
            ListItems(ctx1, "After add");

            ctx1.SaveChanges();
            ListItems(ctx1, "After commit");
        }

        Console.ReadKey();
    }

    public static void ListItems(TinyContext ctx, string label="")
    {
        Console.WriteLine("=========================================");
        Console.WriteLine(label);
        Console.WriteLine(string.Format("Items.Local: {0}", ctx.Items.Local.Count));
        foreach (var item in ctx.Items.Local)
        {
            Console.WriteLine(string.Format("{0} = {1}", item.Id, item.Name));
        }
        Console.WriteLine(string.Format("Items: {0}", ctx.Items.Count()));
        foreach (var item in ctx.Items)
        {
            Console.WriteLine(string.Format("{0} = {1}", item.Id, item.Name));
        }
        Console.WriteLine("=========================================");
    }

First I added one record to the database. Then I ran this and these are the results:

    =========================================
    Start
    Items.Local: 0
    Items: 1
    4 = Test1
    =========================================
    =========================================
    After add
    Items.Local: 2
    4 = Test1
    0 = Test1
    Items: 1
    4 = Test1
    =========================================
    =========================================
    After commit
    Items.Local: 2
    4 = Test1
    5 = Test1
    Items: 2
    4 = Test1
    5 = Test1
    =========================================

My questions are:
– Why does the first call to ctx.Items.Local give my zero items?
– Why does the list of ctx.Items not contain the just added item before I called SaveChanges?

  • 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-23T14:46:32+00:00Added an answer on May 23, 2026 at 2:46 pm

    Why does the first call to
    ctx.Items.Local give my zero items?

    Because EF hasn’t loaded any items from the database(or you haven’t added any). Hence it is not tracking any items yet. That is shown as 0.

    Here’s the msdn description for Local

    Returns ObservableCollection that
    represents entities of the set that
    are currently being tracked by the
    context and have not been marked as
    Deleted. Accessing the Local property
    never causes a query to be sent to the
    database. This property is usually
    used after a query has already been
    executed.


    Why does the list of ctx.Items not
    contain the just added item before I
    called SaveChanges?

    When you refer ctx.Items it will be fetched from database. Since there is only 1 item (you haven’t called the SaveChanges() method) in the database it shows the item(s) in the database.

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

Sidebar

Related Questions

I don't understand why this code doesn't work: function Messages(type,text) { console.log(In function Message);
Don't know a whole lot about streams. Why does the first version work using
I don’t think I’ve grokked currying yet. I understand what it does, and how
Don't want to sort the entries. using this does not preserve the order as
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't let below code scare you away . The question is really simple, only
Don't be scared of the extensive code. The problem is general. I just provided
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't know when to add to a dataset a tableadapter or a query
I don't understand where the extra bits are coming from in this article about

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.