Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

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

Comments

Visual Studio 2010 and .Net Framework 4.0 - Gunnar Peipman's ASP.NET blog said:

Pingback from  Visual Studio 2010 and .Net Framework 4.0 - Gunnar Peipman's ASP.NET blog

# July 11, 2009 1:05 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# July 11, 2009 1:20 PM

DotNetBurner - ADO.NET said:

DotNetBurner - burning hot .net content

# July 11, 2009 1:21 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# July 11, 2009 1:22 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 11, 2009 1:24 PM

Gunnar Peipman's ASP.NET blog said:

In my previous posting Entity Framework 4.0: How to use POCOs I introduced how simple it is to use POCOs

# July 12, 2009 3:18 PM

Entity Framework 4.0: POCOs and table-per-type inheritance mapping | I love .NET! said:

Pingback from  Entity Framework 4.0: POCOs and table-per-type inheritance mapping | I love .NET!

# July 12, 2009 4:01 PM

Entity Framework 4.0: How to use POCOs - Gunnar Peipman's ASP.NET blog said:

Pingback from  Entity Framework 4.0: How to use POCOs - Gunnar Peipman&#39;s ASP.NET blog

# July 12, 2009 6:30 PM

Entity Framework 4.0: POCOs and table-per-type inheritance mapping | Nexo IT - Information Technology News said:

Pingback from  Entity Framework 4.0: POCOs and table-per-type inheritance mapping | Nexo IT - Information Technology News

# July 12, 2009 9:20 PM

Entity Framework 4.0: How to use POCOs – Gunnar Peipman's ASP.NET blog | Webmaster Tools said:

Pingback from  Entity Framework 4.0: How to use POCOs &#8211; Gunnar Peipman&#039;s ASP.NET blog | Webmaster Tools

# July 12, 2009 9:51 PM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - July 9-13, 2009 Web Development Using Custom T4

# July 13, 2009 4:15 AM

Cleveland related services in Northamptonshire said:

Pingback from  Cleveland related services in Northamptonshire

# July 13, 2009 6:22 AM

Thomas Eyde said:

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?

# July 13, 2009 6:44 PM

DigiMortal said:

Thomas, you don't have to subclassing to get POCOs work.

I cite myself: "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."

If you are using Custom Tool that is by default code generator for EF models then you get auto-generated classes. If you do like I suggested (remove custom tool) then there will be no more auto-generated classes and you can use ones you have created.

Also I suggest to read blog entries I listed in the end of this posting. These are great examples that show you step-by-step what to do.

# July 14, 2009 3:13 AM

VS2010学习 said:

Entity Framework 4.0: POCOs and table-per-type inheritance mapping In my previous posting Entity Framework

# July 14, 2009 1:11 PM

Gunnar Peipman's ASP.NET blog said:

Entity Framework 4.0 is able to generate database schema based on model. If you built your model first

# July 23, 2009 12:44 PM

Community Blogs said:

Entity Framework 4.0 is able to generate database schema based on model. If you built your model first

# July 23, 2009 12:57 PM

Gunnar Peipman's ASP.NET blog said:

Entity Framework 4.0 is able to generate database schema based on model. If you built your model first

# July 23, 2009 1:03 PM

Gunnar Peipman's ASP.NET blog said:

MyGallery2010 is my simple photo gallery system built on Visual Studio 2010 and .Net Framework 4.0 technologies

# July 24, 2009 11:01 AM

Learning resources for Entity Framework 4.0 new features « Bogdan Brinzarea’s blog said:

Pingback from  Learning resources for Entity Framework 4.0 new features &laquo;  Bogdan Brinzarea&#8217;s blog

# August 5, 2009 5:23 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)