0

我覆盖了 SaveChanges 方法来审核 EntityFramework 中的更改。在我尝试更新关联对象之前,它工作正常。当我尝试更新关联对象时,我得到 ArgumentException。我发现从 ChangeTracker 读取实体后会引发异常。如果我在覆盖的 SaveChanges 方法中对 ChangeTracker 不执行任何操作,则对象将成功更新。我使用 EntityFramework 5.0。

你不知道这是一个错误还是我做错了什么?

抛出异常:

System.ArgumentException 被捕获 HResult=-2147024809
Message=定义 EntityKey 的键值对不能为 null 或空。参数名称:记录 Source=System.Data.Entity
ParamName=record StackTrace:在 System.Data.EntityKey.GetKeyValues(EntitySet entitySet, IExtendedDataRecord record, String[]& keyNames, Object& singletonKeyValue, Object[]&compositeKeyValues) at System.Data .EntityKey..ctor(EntitySet entitySet, IExtendedDataRecord 记录) 在 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(Func2 谓词)在 System.Data.Entity.Infrastructure.DbChangeTracker.Entries() 的 System.Data.Entity.Internal.InternalContext.GetStateEntries() 在 System.Data.Entity.DbContext.GetValidationErrors() 在 System.Data.Entity。 Internal.InternalContext.SaveChanges() 在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 在 System.Data.Entity.DbContext.SaveChanges() 在 EFTest2.BlogContext.SaveChanges() 在 c:\Projects\EF22\EFTest2Solution \EFTest2\Context.cs:EFTest2.Program.Main(String[] args) 中的第 37 行:\Projects\EF22\EFTest2Solution\EFTest2\Program.cs:第 42 行

我的代码是这样的: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
      }
    }
  }

模型

  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");
    }
  }

语境:

  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();
    }
  }

配置

  public class Configuration : DbMigrationsConfiguration<BlogContext>
  {
    public Configuration()
    {
      AutomaticMigrationsEnabled = true;
      AutomaticMigrationDataLossAllowed = true;
    }
  }
4

1 回答 1

0

来自 msdn 的反应......这是一个错误。要解决此问题,必须使用不可为空的主键。

EF codeplex 站点报告了错误:使用可为空的主键调用两次检测更改时引发的异常

于 2012-11-16T05:49:21.140 回答