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

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

问题描述

我正试图附加一个通过已连接到特定的上下文对象时出现以下错误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(...)

干杯!

编辑:

扩展方法贾森概述接近,但它并没有对我的工作的情况。

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:

<一个href="http://stackoverflow.com/questions/1601350/how-do-i-delete-one-or-more-rows-from-my-table-using-linq-to-entities-without-r">http://stackoverflow.com/questions/1601350/how-do-i-delete-one-or-more-rows-from-my-table-using-linq-to-entities-without-r

我的code看起来有点像这样的:

My code looks a bit like this:

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

这工作得很好,只是当我做别的东西,我用同样的方法,并尝试将一个虚拟的用户对象的用户。这将失败,因为我有previously连接的虚拟用户对象。我如何检查呢?

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招我提到每次上面。你最终与任何对象previously连接或自己的对象被连接。呼叫 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天全站免登陆