【问题标题】:Why does the ObjectStateManager property not exist in my db context?为什么我的数据库上下文中不存在 ObjectStateManager 属性?
【发布时间】:2012-11-27 09:51:35
【问题描述】:

我需要从我的数据库上下文中返回一个新添加对象的列表。

我已经读到我必须为此目的使用ObjectStateManager。问题是,我的数据库上下文没有 ObjectStateManager 属性。

尽管上下文可以很好地检索、添加和更新对象。

我正在使用 EF 5.0

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

我能做什么?

【问题讨论】:

  • “我必须为此使用 ObjectStateManager。” -- 如您所见,ObjectStateManager 仍然有效,但您不需要它。 DbContext 派生类可以使用 ChangeTracker 属性来获取该信息。

标签: c# entity-framework


【解决方案1】:

试试这个:

var manager = ((IObjectContextAdapter)dbContext).ObjectContext.ObjectStateManager;

【讨论】:

  • 在 netcore 中找不到 IObjectContextAdapter?
【解决方案2】:

试试这个:

dbContext.Entry(entity).State = EntityState.Modified;

【讨论】:

  • 这不完全是答案,但帮助我解决了一个问题
猜你喜欢
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2019-09-23
  • 2012-02-10
  • 2015-09-18
  • 2016-03-01
  • 2012-07-05
  • 2012-10-03
相关资源
最近更新 更多