tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Attaching non-detached entity in Entity Framework v4 doesn’t throw exception

23 Oct 2009 1 mins Entity Framework

While slowly digging into Beta 2 of EF4 I discovered new nice “feature”. In EF1, if you had code like this.

master[] data;
using (testovaciEntities ent = new testovaciEntities())
{
	data = ent.masters.ToArray();
}
using (testovaciEntities ent = new testovaciEntities())
{
	foreach (master item in data)
	{
		ent.Attach(item);
	}
}

It threw InvalidOperationException saying An entity object cannot be referenced by multiple instances of IEntityChangeTracker.. Right but I don’t have access to my previous context anymore. This behavior caused me a lot of headache and I created couple of hacks to workaround it. But the good news is that the code above works fine in EF4.

Neat! Together with other improvements I can get rid of my hacks and sleep well again. 😉

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.