Entity Framework 4.0: How to use POCOs

Entity Framework 4.0 will provide us with POCO support. That’s good because Entity Framework supports more inheritance mapping strategies than LINQ To SQL but currently it doesn’t support POCOs. To try out Visual Studio 2010 and ASP.NET 4.0 new features I started writing simple photo gallery web application. Today I got my first simple POCOs work with Entity Framework 4.0.

As a first thing let’s see my two classes – Album and Photo. Don’t expect any architecture or design miracles here, it’s only example focusing on Entity Framework 4.0.

Album is complex when comparing with Photo because Album has child albums and photos collections. Only complex thing that Photo has is reference to parent album. As you can see I mapped these classes through entity model designer.


public class Album : IGalleryItem
{
    public virtual int Id { get; set; }
    public virtual IList<Album> ChildAlbums { get; set; }
    public virtual string Description { get; set; }
    public virtual Album ParentAlbum { get; set; }
    public virtual IList<Photo> Photos { get; set; }
    public virtual string Title { get; set; }
    public virtual bool Visible { get; set; }
}
 
public class Photo : IGalleryItem
{
    public virtual int Id { get; set; }
    public virtual Album Album { get; set; }
    public virtual string Description { get; set; }
    public virtual string FileName { get; set; }
    public virtual bool IsGalleryThumb { get; set; }
    public virtual string Title { get; set; }
    public virtual bool Visible { get; set; }
}

Now we have two more steps to do.

First step is to turn off code generation. Move to your model file in Solution Explorer and press F4 on it to see properties. There is property called Custom Tool (look at the picture on right). Make sure you clear the value of this property and save the model file.

Second step is to create our own object context class that extends ObjectContext. In this class we have to define all the collections we want to use in LINQ queries.

Currently I have implemented no logic for data modification operations but context class mentioned before has methods you can use to modify data of your objects.

Let’s see my simple context class now.


public class MyGalleryEntities : ObjectContext
{
    private ObjectSet<Album> _albums;
    private ObjectSet<Photo> _photos;
 
    public MyGalleryEntities() : base("Name=MyGalleryEntities")
    {
        DefaultContainerName = "MyGalleryEntities";
        ContextOptions.DeferredLoadingEnabled = true;
        _albums = CreateObjectSet<Album>("MyGalleryEntities.Albums");
        _photos = CreateObjectSet<Photo>("MyGalleryEntities.Photos");
    }
 
    public ObjectSet<Album> Albums
    {
        get { return _albums; }
    }
 
    public ObjectSet<Photo> Photos
    {
        get { return _photos; }
    }
}

Take a look at constructor of MyGalleryEntities class. I was not able to get object sets created without these names. You can find the name to use in your model. Just open it, press F4 to see model properties and look at Entity Container Name property.

Here are some example queries.


public void Examples()
{
    var context = new MyGalleryEntities();
 
    var visibleAlbums = from a in context.Albums
                        where a.Visible && a.Photos.Count > 0
                        select a;
 
    var searchPhotos = from p in context.Photos
                       where p.Title.StarsWith("Bunny")
                       select p;
}

As we saw, using POCOs in Entity Framework 4.0 is pretty simple. I suggest you to read also the following entries from ADO.NET team blog:


kick it on DotNetKicks.com pimp it Progg it Shout it

2 Comments

  • I must be missing something, because I don't find this easy at all: I have to subclass and implement something if I want POCOs?

    One of the important features, if not the most, of EF 4 is its support for POCOs. What kind of support is this, when I can't get it out of the box?

  • In my blog, I posted a performance comparison between POCO classes and auto generated code, checked out.

Comments have been disabled for this content.