tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

POCOs and Entity Framework’s automagic association wiring

10 Apr 2010 2 mins Entity Framework

Entity Framework has a nice ability to wire up associations on both ends when the related entities come into context. This feature is handy and you can use it to simulate some scenarios, like this. But we’re on EFv4 time, everybody is using POCO. 😉

In last project I used POCO too, just to make myself more familiar with the way how it works in EFv4 and to face some challenges. Last week I was designing some flow, and I needed to have some related objects in memory too. I immediately jumped into Include method (actually I used this improved version). But later I couldn’t help but wonder would the automagic association wiring with (pure) POCOs?

I created simple master-detail scenario and created these POCO classes (note I’m not going to use proxies).

class Master
{
	public int ID { get; set; }
	public string Foo { get; set; }
	public ICollection<Detail> Details { get; set; }
	public Master()
	{
		this.Details = new List<Detail>();
	}
}
class Detail
{
	public int ID { get; set; }
	public string Bar { get; set; }
	public Master Master { get; set; }
}

And created test code (testEntities is a simple ObjectContext).

using (testEntities ent = new testEntities())
{
	ent.ContextOptions.LazyLoadingEnabled = false;
	var details = ent.Details.ToArray();
	var q = ent.Masters;
	foreach (var item in q)
	{
		Console.WriteLine(item.Details.Count);
	}
	foreach(var item in details)
	{
		Console.WriteLine(item.Master.ID);
	}
}

If you run the code, you’ll see it works as expected. Exactly like the generated classes, although these are (pure) POCO classes – no need to use proxies or any special collections. I like the smart work done behind by Entity Framework for me.

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.