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 8858705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:53:20+00:00 2026-06-14T14:53:20+00:00

I overrode SaveChanges method to audit changes in EntityFramework. That works fine until I

  • 0

I overrode SaveChanges method to audit changes in EntityFramework. That works fine until I try to update associated object. When I try to update associated object, I get ArgumentException. I found out that exception is thrown after I read entities from ChangeTracker. If I do not do nothing with ChangeTracker in overriden SaveChanges method, object is succesfully updated. I use EntityFramework 5.0.

Don´t you know please if that is a bug or I´m doing something wrong?

Thrown exception:

System.ArgumentException was caught HResult=-2147024809
Message=The key-value pairs that define an EntityKey cannot be null or
empty. Parameter name: record Source=System.Data.Entity
ParamName=record StackTrace:
at System.Data.EntityKey.GetKeyValues(EntitySet entitySet, IExtendedDataRecord record, String[]& keyNames, Object&
singletonKeyValue, Object[]& compositeKeyValues)
at System.Data.EntityKey..ctor(EntitySet entitySet, IExtendedDataRecord record)
at System.Data.Objects.ObjectStateManager.PerformDelete(IList1 entries)
at System.Data.Objects.ObjectStateManager.DetectChanges()
at System.Data.Objects.ObjectContext.DetectChanges()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean
force)
at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func
2
predicate)
at System.Data.Entity.Internal.InternalContext.GetStateEntries()
at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
at System.Data.Entity.DbContext.GetValidationErrors()
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at EFTest2.BlogContext.SaveChanges() in c:\Projects\EF22\EFTest2Solution\EFTest2\Context.cs:line 37
at EFTest2.Program.Main(String[] args) in c:\Projects\EF22\EFTest2Solution\EFTest2\Program.cs:line 42

My code looks this way:
Program.cs

  class Program
  {
    static void Main(string[] args)
    {
      var configuration = new Configuration();
      configuration.TargetDatabase = new DbConnectionInfo("Data Source=server;Initial Catalog=EntityFramework;Integrated Security=True", "System.Data.SqlClient");
      var migrator = new DbMigrator(configuration);

      var scriptor = new MigratorScriptingDecorator(migrator);
      var script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);
      Console.WriteLine(script);

      var pending = migrator.GetPendingMigrations();

      migrator.Update();

      using (var ctx = new BlogContext())
      {
        Post post = new Post { Content = "Content", SpecialBlog = new Blog() { Title = "SpecialBlog" } };
        ctx.Posts.Add(post);
        ctx.SaveChanges();

        post.SpecialBlog = new Blog() { Title = "Update SpecialBlog" };

        ctx.SaveChanges(); // ArgumentException is thrown when using ChangeTracker
      }
    }
  }

Model

  public class Blog
  {
    public int? BlogId { get; set; }
    public string Title { get; set; }
    public ICollection<Post> Posts { get; set; }
  }

  public partial class BlogMap : EntityTypeConfiguration<Blog>
  {
    public BlogMap()
      : base()
    {
      HasKey(c => c.BlogId);
      Property(c => c.Title).IsRequired().HasMaxLength(40);
      ToTable("Blog");
    }
  }

  public class Post 
  {
    public int? PostId { get; set; }
    public string Content { get; set; }

    public int? BlogId { get; set; }
    public Blog Blog { get; set; }
    public int? SpecialBlogId { get; set; }
    public Blog SpecialBlog { get; set; }
  }

  public partial class PostMap : EntityTypeConfiguration<Post>
  {
    public PostMap()
      : base()
    {
      HasKey(c => c.PostId);

      Property(c => c.Content);

      HasOptional(m => m.Blog)
                    .WithMany(t => t.Posts)
                    .HasForeignKey(m => m.BlogId)
                    .WillCascadeOnDelete(false);

      HasOptional(m => m.SpecialBlog)
                    .WithMany()
                    .HasForeignKey(m => m.SpecialBlogId)
                    .WillCascadeOnDelete(false);

      ToTable("Post");
    }
  }

Context:

  public class BlogContext : DbContext
  {
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      Database.SetInitializer(new DropCreateDatabaseAlways<BlogContext>());

      modelBuilder.Configurations.Add(new PostMap());
      modelBuilder.Configurations.Add(new BlogMap());
    }

    public override int SaveChanges()
    {
      var entries = ChangeTracker.Entries(); // causes ArgumentNullException after calling base.SaveChanges();

      return base.SaveChanges();
    }
  }

Configuration

  public class Configuration : DbMigrationsConfiguration<BlogContext>
  {
    public Configuration()
    {
      AutomaticMigrationsEnabled = true;
      AutomaticMigrationDataLossAllowed = true;
    }
  }
  • 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-06-14T14:53:21+00:00Added an answer on June 14, 2026 at 2:53 pm

    Reaction from msdn… it is a bug. To fix it is necessary to use non-nullable primary keys.

    Bug is reported at EF codeplex site: Exception thrown when calling detect changes twice with nullable primary keys

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

Sidebar

Related Questions

I've overrode the method find for ActiveRecord::Base and that went pretty well, now I
I've subclassed QAbstractTableModel and overrode the flags() method so that some of the table
For audit logging purpose I override SaveChanges() method in EF 4.1 Database-First approach .
I am using EF4 CTP5 to try to persist a POCO object that is
When placing an icon on a page, it seems that it somehow overrode the
I have an entity model that has audit information on every table (50+ tables)
I subclassed uiscrollview and overrode the touch began method to pass touch events to
I'm new to Python, and I wanted to make sure that I overrode __eq__
i'm using EF 4.3.1, I've overridden the SaveChanges() on the context so that I
To introduce myself to the entity framework I created a console application that works

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.