tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

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

23 Oct 2009 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 an independent developer focusing on data and business layers, language constructs, parallelism and databases. Specifically Entity Framework, asynchronous and parallel programming, cloud and Azure. He's Microsoft Most Valuable Professional and you can read his articles, guides, tips and tricks at www.tabsoverspaces.com.