实体框架自我跟踪实体 - 调用 StartTracking 时出错

发布于 2025-01-07 09:12:53 字数 889 浏览 0 评论 0原文

我将自我跟踪实体与实体框架结合使用,由于某种原因,在对实体进行更改之前调用 StartTracking() 时,我现在收到错误。

我的代码如下:

BusinessUnit BusinessUnitObject = this.settingFacade.GetBusinessUnitByID(idToGet);
BusinessUnitObject.StartTracking();

所有这一切都是使用我们拥有的 ID,使用对象上下文从数据库中读取,然后立即开始跟踪它。

错误是

Value cannot be null. Parameter name: trackingItem

在查看实体框架的实际生成代码时,抛出错误的代码是:

public static void StartTracking(this IObjectWithChangeTracker trackingItem)
{
    if (trackingItem == null)
    {
        throw new ArgumentNullException("trackingItem");
    }

    trackingItem.ChangeTracker.ChangeTrackingEnabled = true;
}

参数“trackingItem”为空,但我不确定为什么。

以前有人遇到过这个吗?过去几个月我一直在使用 STE,这是第一次发生这种情况。

编辑 -------------------

抱歉,各位......在对我的代码进行了大量重构之后,我引入了一个错误,以及对象本身为 null,然后我调用 StartTracking()!

I am using Self Tracking Entities with the Entity Framework and for some reason I am now getting an error when making a call to StartTracking() before I make changes to an entity.

My code is as follows:

BusinessUnit BusinessUnitObject = this.settingFacade.GetBusinessUnitByID(idToGet);
BusinessUnitObject.StartTracking();

All this does, is use an ID that we have, use the object context to read from the database, and then start tracking on it straight away.

The error is

Value cannot be null. Parameter name: trackingItem

Upon looking at the actual generated code of Entity Framework, the code throwing the error is:

public static void StartTracking(this IObjectWithChangeTracker trackingItem)
{
    if (trackingItem == null)
    {
        throw new ArgumentNullException("trackingItem");
    }

    trackingItem.ChangeTracker.ChangeTrackingEnabled = true;
}

The parameter "trackingItem" is null, but I am unsure why.

Has anyone come across this before? I have been using STE's the last couple of months and this is the first time this has happened.

EDIT -------------------

Sorry folks...after a lot of refactoring of my code I have introduced a bug, and the object itself was null, which I was then calling StartTracking() on!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文