Geeks With Blogs

News

Currently Reading

CLR via C#m
Under The Influence(of code) Abhijeet Patel's blog

Recently I've started working more and more with the ADO.NET Entity framework and I must say that I like it a lot. There are folks who absolutely love EDM and folks that think its really evil.
It's still a v1.0 product and I treat it as such. The designer does leave a lot to be desired.A lot of bells and whistles will be added to the VS 2010 release to address a lot of the designer "desirables".

It's not been an easy buy in at work for adopting EDM, but my team has been among the first to have adopted it (against some odds of course - <nudge >DBAs).

I've found myself doing the same repetitve tasks working with EDM such as adding a collection of items to an EntityCollection, lazily loading entities, converting enumerables to EntityCollections etc.

So, I decided to whip up some extension methods to address the most common tasks that I do from day to day working in EDM. I'll be updating this list as I come up with more. So here goes....

 
  1. public static class EDMExtensions  
  2. {  
  3.   
  4.     /// <summary>  
  5.     /// Performs the specified <see cref="Action&lt;T&gt;"/> on each item of the specified collection  
  6.     /// </summary>  
  7.     /// <typeparam name="T">The type of element in the collection.</typeparam>  
  8.     /// <param name="collection">The collection on whose items the <see cref="&lt;Action&gt;"/> is to be performed.</param>  
  9.     /// <param name="action">The action to perform on each item.</param>  
  10.     public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) where T : class, IEntityWithRelationships  
  11.     {  
  12.         foreach (var item in collection)  
  13.         {  
  14.             action(item);  
  15.         }  
  16.     }  
  17.   
  18.     /// <summary>  
  19.     /// Lazily loads each item of the specifed <see cref="EntityCollection&lt;T&gt;"/>.  
  20.     /// </summary>  
  21.     /// <typeparam name="T">The type of element in the collection.</typeparam>.  
  22.     /// <param name="collection">The collection on whose items the <see cref="&lt;Action&gt;"/> is to be performed.</param>  
  23.     /// <returns></returns>  
  24.     public static EntityCollection<T> LazyLoad<T>(this EntityCollection<T> collection) where T : class, IEntityWithRelationships  
  25.     {  
  26.         if (!collection.IsLoaded)  
  27.         {  
  28.             collection.Load();  
  29.         }  
  30.         return collection;  
  31.     }  
  32.   
  33.   
  34.     /// <summary>  
  35.     /// Lazily loads the item referenced by the specified <see cref="EntityReference&lt;T&gt;"/>.  
  36.     /// </summary>  
  37.     /// <typeparam name="T">The type of element in the collection.</typeparam>.  
  38.     /// <param name="reference">The entity reference.</param>  
  39.     public static void LazyLoad<T>(this EntityReference<T> reference) where T : class, IEntityWithRelationships  
  40.     {  
  41.         if (!reference.IsLoaded)  
  42.         {  
  43.             reference.Load();  
  44.         }  
  45.     }  
  46.   
  47.   
  48.     /// <summary>  
  49.     /// Adds a collection of items to the specified <see cref="EntityCollection&lt;T&gt;"/>  
  50.     /// </summary>  
  51.     /// <typeparam name="T"></typeparam>  
  52.     /// <param name="collection">The collection.</param>  
  53.     /// <param name="items">The items.</param>  
  54.     public static void Add<T>(this EntityCollection<T> collection, IEnumerable<T> items) where T : class, IEntityWithRelationships  
  55.     {  
  56.         foreach (var item in items)  
  57.         {  
  58.             collection.Add(item);  
  59.         }  
  60.     }  
  61.   
  62.   
  63.     /// <summary>  
  64.     /// Converts the specifed enumerable to an <see cref="EntityCollection&lt;T&gt;"/>.  
  65.     /// </summary>  
  66.     /// <typeparam name="T"></typeparam>  
  67.     /// <param name="items">The items to be converted to an  <see cref="EntityCollection&lt;T&gt;"/>.</param>  
  68.     /// <returns>The generated <see cref="EntityCollection&lt;T&gt;"/></returns>  
  69.     public static EntityCollection<T> ToEntityCollection<T>(this IEnumerable<T> items) where T : class, IEntityWithRelationships  
  70.     {  
  71.         EntityCollection<T> coll = new EntityCollection<T>();  
  72.         foreach (var item in items)  
  73.         {  
  74.             coll.Add(item);  
  75.         }  
  76.         return coll;  
  77.     }  
  78.   
  79. }  

kick it on DotNetKicks.com
Posted on Thursday, March 5, 2009 7:43 PM C# , Entity Framework | Back to top


Comments on this post: Some Extension Methods for working with the ADO.NET Entity Framework

# re: Some Extension Methods for working with the ADO.NET Entity Framework
Requesting Gravatar...
Hi there,

Thanks for these extension methods, it's really useful!

Just an issue:

I get a System.InvalidOperationException on the ToEntityCollection method :

The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object.

Do you know how to fix it ?

Thanks a lot
Left by Benjiiim on Aug 10, 2009 8:35 AM

# re: Some Extension Methods for working with the ADO.NET Entity Framework
Requesting Gravatar...
Hey Ben,
Glad you find them useful. I noticed this issue when the source collection on which ToEntityCollection() is called on is an IQueryable<T> which is returned from an ObjectContext. I suspect that this is the scenario in which you are noticing this error. It seems that it is not legal in the Entity Framework to add items which are currently attached to an ObjectContext and add them "into" a newly created Entitycollection.
However, if you can "explicity" 'Detach' the objects from the ObjectContext and then call 'ToEntityCollection()' you would be able to get by.

Here is an example:

var movies = Context.MovieSet.Take(3).ToList() ;
movies.ForEach(m => Context.Detach(m));
var newEntityColl = movies.ToEntityCollection();

Hope this helps.
Left by Abhijeet on Aug 11, 2009 9:19 PM

# re: Some Extension Methods for working with the ADO.NET Entity Framework
Requesting Gravatar...
That helped me!
Thanks a lot!
Left by Eduardo on Feb 06, 2011 1:04 PM

# re: Some Extension Methods for working with the ADO.NET Entity Framework
Requesting Gravatar...
Great article, but what about the EF 4.1+ and DbSet?
Left by Mustafa Magdy on Nov 21, 2011 6:22 AM

Your comment:
 (will show your gravatar)


Copyright © Abhijeet Patel | Powered by: GeeksWithBlogs.net