是否可以检查对象是否已附加到实体框架中的数据上下文? [英] Is is possible to check if an object is already attached to a data context in Entity Framework?

查看:30
本文介绍了是否可以检查对象是否已附加到实体框架中的数据上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试通过 context.AttachTo(...) 附加已附加到给定上下文的对象时,出现以下错误:

I am getting the following error when trying to attach an object that is already attached to a given context via context.AttachTo(...):

ObjectStateManager 中已存在具有相同键的对象.ObjectStateManager 无法跟踪具有相同键的多个对象.

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

有没有办法实现以下目标:

Is there a way of achieving something along the lines of:

context.IsAttachedTo(...)

干杯!

Jason 概述的扩展方法很接近,但它不适用于我的情况.

The extension method Jason outlined is close, but it doesn't work for my situation.

我正在尝试使用另一个问题的答案中概述的方法做一些工作:

I am trying to do some work using the method outlined in the answer to another question:

如何使用 Linq to Entities 删除表中的一行或多行*而不*先检索行?

我的代码看起来有点像这样:

My code looks a bit like this:

var user = new User() { Id = 1 };
context.AttachTo("Users", user);
comment.User = user;
context.SaveChanges();

这很好用,除非我为那个用户做其他事情,我使用相同的方法并尝试附加一个虚拟的 User 对象.这失败了,因为我之前已经附加了那个虚拟用户对象.我该如何检查?

This works fine, except when I do something else for that user where I use the same method and try to attach a dummy User object. This fails because I have previously attached that dummy user object. How can I check for this?

推荐答案

这是我最后的结果,效果很好:

Here's what I ended up with, which works very nicely:

public static void AttachToOrGet<T>(this ObjectContext context, string entitySetName, ref T entity)
    where T : IEntityWithKey
{
    ObjectStateEntry entry;
    // Track whether we need to perform an attach
    bool attach = false;
    if (
        context.ObjectStateManager.TryGetObjectStateEntry
            (
                context.CreateEntityKey(entitySetName, entity),
                out entry
            )
        )
    {
        // Re-attach if necessary
        attach = entry.State == EntityState.Detached;
        // Get the discovered entity to the ref
        entity = (T)entry.Entity;
    }
    else
    {
        // Attach for the first time
        attach = true;
    }
    if (attach)
        context.AttachTo(entitySetName, entity);
}

你可以这样称呼它:

User user = new User() { Id = 1 };
II.AttachToOrGet<Users>("Users", ref user);

这非常有效,因为它就像 context.AttachTo(...) 不同之处在于你每次都可以使用我上面提到的 ID 技巧.您最终会得到先前附加的对象或您自己的对象.在上下文中调用 CreateEntityKey 可确保它很好且通用,甚至可以使用复合键而无需进一步编码(因为 EF 已经可以为我们做到这一点!).

This works very nicely because it's just like context.AttachTo(...) except you can use the ID trick I cited above each time. You end up with either the object previously attached or your own object being attached. Calling CreateEntityKey on the context makes sure it's nice and generic and will work even with composite keys with no further coding (because EF can already do that for us!).

这篇关于是否可以检查对象是否已附加到实体框架中的数据上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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