<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx</link><description>Background: 
 In order to be an EF power user today you really need to be familiar with EntitySets . For example you need to understand EntitySets in order to use AttachTo(…) or create EntityKeys . 
 In most cases there is only one possible EntitySet</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9793921</link><pubDate>Sat, 20 Jun 2009 20:58:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793921</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;Martin,&lt;/p&gt;
&lt;p&gt;That is pretty cool. And works well with EntityObject. Bear in mind though that your cool solution won't work if you convert to POCO classes in .NET 4.0&lt;/p&gt;
&lt;p&gt;Did you look at the other implementation of GetEntitySet name that I wrote? The one I did in Tip13. That is more general purpose, and will work in POCO classes too.&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9793921" width="1" height="1"&gt;</description></item><item><title>re: Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9793303</link><pubDate>Sat, 20 Jun 2009 12:55:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793303</guid><dc:creator>Martin Robins</dc:creator><description>&lt;p&gt;As an alternative to selecting the entity set name from the query, I created the following extension method based upon the thread at &lt;a rel="nofollow" target="_new" href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3fc700f6-30de-4346-b1ed-09df8c21c273"&gt;http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3fc700f6-30de-4346-b1ed-09df8c21c273&lt;/a&gt; ...&lt;/p&gt;
&lt;p&gt;public static string GetEntitySetName&amp;lt;T&amp;gt;(this ObjectContext ctx) where T : EntityObject {&lt;/p&gt;
&lt;p&gt;	EdmEntityTypeAttribute attribute = ((EdmEntityTypeAttribute[])typeof(T).GetCustomAttributes(typeof(EdmEntityTypeAttribute), true)).Single();&lt;/p&gt;
&lt;p&gt;	EntityContainer container = ctx.MetadataWorkspace.GetEntityContainer(ctx.DefaultContainerName, DataSpace.CSpace);&lt;/p&gt;
&lt;p&gt;	return container.BaseEntitySets.Single(es =&amp;gt; es.ElementType.Name == attribute.Name).Name;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;this allows the entity set to be determined based upon the attributes in the model and can be used as follows...&lt;/p&gt;
&lt;p&gt;public static void AddObject&amp;lt;T&amp;gt;(this ObjectQuery&amp;lt;T&amp;gt; query, T entity) where T : EntityObject {&lt;/p&gt;
&lt;p&gt;	query.Context.AddObject(query.Context.GetEntitySetName&amp;lt;T&amp;gt;(), entity);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;public static void Attach&amp;lt;T&amp;gt;(this ObjectQuery&amp;lt;T&amp;gt; query, T entity) where T : EntityObject {&lt;/p&gt;
&lt;p&gt;	query.Context.AttachTo(query.Context.GetEntitySetName&amp;lt;T&amp;gt;(), entity);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9793303" width="1" height="1"&gt;</description></item><item><title>re: Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9793292</link><pubDate>Sat, 20 Jun 2009 12:49:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793292</guid><dc:creator>Martin Robins</dc:creator><description>&lt;p&gt;As an alternative to selecting the entity set name from the query, I created the following extension method based upon the thread at &lt;a rel="nofollow" target="_new" href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3fc700f6-30de-4346-b1ed-09df8c21c273"&gt;http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3fc700f6-30de-4346-b1ed-09df8c21c273&lt;/a&gt; ...&lt;/p&gt;
&lt;p&gt;public static string GetEntitySetName&amp;lt;T&amp;gt;(this ObjectContext ctx) where T : EntityObject {&lt;/p&gt;
&lt;p&gt;			EdmEntityTypeAttribute attribute = ((EdmEntityTypeAttribute[])typeof(T).GetCustomAttributes(typeof(EdmEntityTypeAttribute), true)).Single();&lt;/p&gt;
&lt;p&gt;			EntityContainer container = ctx.MetadataWorkspace.GetEntityContainer(ctx.DefaultContainerName, DataSpace.CSpace);&lt;/p&gt;
&lt;p&gt;			return container.BaseEntitySets.Single(es =&amp;gt; es.ElementType.Name == attribute.Name).Name;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9793292" width="1" height="1"&gt;</description></item><item><title>Tip 26 – How to avoid database queries using Stub Entities</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9792664</link><pubDate>Sat, 20 Jun 2009 05:35:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792664</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;What are Stub Entities? A stub entity is a partially populated entity that stands in for the real thing&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9792664" width="1" height="1"&gt;</description></item><item><title>Tip 26 – How to avoid database queries using Stub Entities</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9791407</link><pubDate>Fri, 19 Jun 2009 21:00:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9791407</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;What are Stub Entities? A stub entity is a partially populated entity that stands in for the real thing.&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9791407" width="1" height="1"&gt;</description></item><item><title>re: Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9586124</link><pubDate>Mon, 04 May 2009 20:24:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9586124</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;@dsoltesz&lt;/p&gt;
&lt;p&gt;Absolutely you are right, for add operations, but the point is this is much easier for other operations like Attach(), and it costs very little to extend it to support Add too.&lt;/p&gt;
&lt;p&gt;Also I actually prefer the intellisense experience typing&lt;/p&gt;
&lt;p&gt;ctx.Customers.Add(...&lt;/p&gt;
&lt;p&gt;as opposed to&lt;/p&gt;
&lt;p&gt;ctx.AddToCustomer(...&lt;/p&gt;
&lt;p&gt;where I have to look through all the AddToX(...) methods to get the one I'm after.&lt;/p&gt;
&lt;p&gt;Its a little thing I know but it does make a difference.&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9586124" width="1" height="1"&gt;</description></item><item><title>re: Tip 16 – How to mimic .NET 4.0’s ObjectSet&lt;T&gt; today</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9585896</link><pubDate>Mon, 04 May 2009 17:29:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9585896</guid><dc:creator>dsoltesz</dc:creator><description>&lt;p&gt;Even with 3.5 sp1 you don't have to use the&lt;/p&gt;
&lt;p&gt;ctx.AddObject(“Customers”, newCustomer);&lt;/p&gt;
&lt;p&gt;you can simply say ctx.AddToCustomers(newCustomer);&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9585896" width="1" height="1"&gt;</description></item><item><title>Tip 16 – How to mimic .NET 4.0’s ObjectSet today - Alex James</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9583001</link><pubDate>Sat, 02 May 2009 00:41:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9583001</guid><dc:creator>DotNetShoutout</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9583001" width="1" height="1"&gt;</description></item><item><title>Meta-Me : Entity Framework Tips</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/01/tip-16-how-to-mimic-net-4-0-s-objectset-t-today.aspx#9582606</link><pubDate>Fri, 01 May 2009 19:42:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9582606</guid><dc:creator>Meta-Me : Entity Framework Tips</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx"&gt;http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9582606" width="1" height="1"&gt;</description></item></channel></rss>