什么触发实体框架来修复导航属性? [英] What triggers Entity Framework to fix up a navigation property?

查看:81
本文介绍了什么触发实体框架来修复导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到关于实体框架决定在外键设置时查找正确的相关对象的正确文档。

I couldn't find good documentation on just exactly what makes Entity Framework decide to look up the correct related object when the foreign key is set.

我是使用延迟加载(但不改变跟踪)代理。设置外键,然后获取导航属性值将返回null,即使相关的导航对象已经加载并位于DbContext中。

I'm using lazy loading (but not change tracking) proxies. Setting the foreign key and then getting the navigation property value returns null, even when the related navigation object is already loaded and sitting in the DbContext.

调用DetectChanges工作但似乎很重。有没有其他方法来指导实体框架来做修复?

Calling DetectChanges works but seems heavy. Is there any other way to clue in Entity Framework to do fixup?

推荐答案

你是对的。 DetectChanges 是触发关系修正的方法。如果您希望通过任何原因发生关系修正,您可以调用 DetectChanges

You're right. DetectChanges is the method that triggers relationship fix-up. If you want relationship fix-up to happen for whatever reason, you call DetectChanges.

但<$ c $执行以下方法时,EF本身也会调用c> DetectChanges :


  • DbSet.Add

  • DbSet.Find

  • DbSet.Remove

  • DbSet.Local

  • DbContext.SaveChanges

  • DbSet.Attach

  • DbContext.GetValidationErrors

  • DbContext.Entry

  • DbChangeTracker.Entries

  • 针对DbSet运行任何LINQ查询

  • DbSet.Add
  • DbSet.Find
  • DbSet.Remove
  • DbSet.Local
  • DbContext.SaveChanges
  • DbSet.Attach
  • DbContext.GetValidationErrors
  • DbContext.Entry
  • DbChangeTracker.Entries
  • Running any LINQ query against a DbSet

(从Lerman& Miller的书籍 DbContext ,第60页)

(From Lerman & Miller's book DbContext, p. 60).

如你所见,几乎任何你在EF外键值将调用 DetectChanges ,所以如果以后还有其他有用的东西要做,先做这些事情,并保存一个 DetectChanges call。

As you see, almost anything you will do with EF after setting the foreign key value will call DetectChanges, so if you've got other useful stuff to do afterwards, do these things first and save one DetectChanges call.

这篇关于什么触发实体框架来修复导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆