<?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>Tips and Tricks</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx</link><description>Hopefully if you're reading this you've noticed that I've started a series of Tips recently. The tips will mostly apply to Entity Framework or Data Services. Seeing as I expect to have a lots of tips, it probably makes sense to have some sort of index.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Anith &amp;raquo; Index of Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9509055</link><pubDate>Thu, 26 Mar 2009 03:39:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9509055</guid><dc:creator>Anith &amp;raquo; Index of Tips</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.anith.com/?p=22679"&gt;http://www.anith.com/?p=22679&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9531800</link><pubDate>Sun, 05 Apr 2009 13:05:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9531800</guid><dc:creator>aleem</dc:creator><description>&lt;p&gt;Alex, I have enjoyed perusing through your tips and wanted to request a post on serialization. I recently switched from L2S to EF and now considering switching back because of serialization issues.&lt;/p&gt;
&lt;p&gt;Primarily, when using L2S, I could serialize the returned object as JSON (or XML for that matter) and get simply: { &amp;quot;Id&amp;quot;:1, &amp;quot;Name&amp;quot;:&amp;quot;John Doe&amp;quot; } but after switching to EF I am stuck with { &amp;quot;Id&amp;quot;:1, &amp;quot;Name&amp;quot;:&amp;quot;John Doe&amp;quot;, &amp;quot;EntityState&amp;quot;:&amp;quot;...&amp;quot;, &amp;quot;EntityKey&amp;quot;:&amp;quot;...&amp;quot;, ... }&lt;/p&gt;
&lt;p&gt;And the work around to fetch and return objects as Anonymous: (from ... where ... select new { Id : t.Id, Name : t.Name }).First(); which is obviously undesirable because it means having to specify each field and is quite rigid by design. Any best practices or recommendations?&lt;/p&gt;</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9532364</link><pubDate>Sun, 05 Apr 2009 23:00:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9532364</guid><dc:creator>AlexJ</dc:creator><description>&lt;p&gt;Aleem,&lt;/p&gt;
&lt;p&gt;Interesting question. I haven't really thought about this before. I'll put it on my list of possible posts.&lt;/p&gt;
&lt;p&gt;PS. Looked at your blog, you've got a lot of comment spam you should look at tidying up&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9534704</link><pubDate>Tue, 07 Apr 2009 02:22:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9534704</guid><dc:creator>aleem</dc:creator><description>&lt;p&gt;Well as it turns out the quick way around it is to return anonymous objects instead of entity objects and use projections.&lt;/p&gt;
&lt;p&gt;&amp;gt; philha@microsofxxxxx wrote:&lt;/p&gt;
&lt;p&gt;&amp;gt; The general approach we recommend is to control the object you're sending to the browser. Instead of sending the full object you grabbed from the db, you want to perhaps send a projection. For example:&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; Instead of&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var product = db.Products.Single(...);&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return View(product);&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; You could send an anonymous object with only the fields you want...&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var product = db.Products.Single(...);&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;var jsonProduct = new {Name = product.Name, Price = product.Price};&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return View(product);&lt;/p&gt;
&lt;p&gt;Not fond of that approach because it requires extra housekeeping (update projects if db schema or field names change and require explicitly specifying all fields). I've fallen back on EFPocoAdapter for now.&lt;/p&gt;
&lt;p&gt;Thanks for pointing out the comment spam. Haven't used that blog for years. Cleaned up and updated link.&lt;/p&gt;</description></item><item><title>Tip 13 - How to Attach an Entity the easy way</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9551490</link><pubDate>Thu, 16 Apr 2009 00:11:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9551490</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;The Problem: In some of earlier tips we talked about using Attach to load things into the ObjectContext&lt;/p&gt;
</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9553313</link><pubDate>Thu, 16 Apr 2009 21:34:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9553313</guid><dc:creator>AlexJ</dc:creator><description>&lt;p&gt;Aleem,&lt;/p&gt;
&lt;p&gt;I was talking to someone today who talked about a WCF interface called IExtensibleDataObject (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iextensibledataobject.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iextensibledataobject.aspx&lt;/a&gt;) that maybe we could use in conjunction with a new T4 template, to hide away EntityKeys etc from the Entity Sent over the wire?&lt;/p&gt;
&lt;p&gt;Anyway just an idea.&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
</description></item><item><title>Tip 19 – How to use Optimistic Concurrency in the Entity Framework</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9630576</link><pubDate>Wed, 20 May 2009 04:20:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9630576</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;This is the 19th post in my ongoing series of Entity Framework Tips . Background: If you have a table&lt;/p&gt;
</description></item><item><title>Tip 20 – How to deal with Fixed Length Keys</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9631483</link><pubDate>Wed, 20 May 2009 08:14:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9631483</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;This is 20th post in my ongoing series of Entity Framework Tips . Fixed Length Field Padding: If you&lt;/p&gt;
</description></item><item><title>Tip 21 - The Single() operator – EF 4.0 only</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9633374</link><pubDate>Thu, 21 May 2009 09:27:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9633374</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;This is 21st post in my ongoing series of Entity Framework Tips , and the first that is specific to EF&lt;/p&gt;
</description></item><item><title>Tip 23 – How to fake Enums in EF 4</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9701907</link><pubDate>Fri, 05 Jun 2009 23:12:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9701907</guid><dc:creator>Meta-Me</dc:creator><description>&lt;p&gt;As of right now Enums are not in EF4. Now we will be listening to your feedback about Beta1, and making&lt;/p&gt;
</description></item><item><title>Entity Framework Tips by Alex James</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9726026</link><pubDate>Thu, 11 Jun 2009 10:12:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9726026</guid><dc:creator>Gil Fink on .Net</dc:creator><description>&lt;p&gt;Entity Framework Tips by Alex James Lately I ran into Alex James blog ( Meta Me ) and into his Entity&lt;/p&gt;
</description></item><item><title>Tip 25 – How to Get Entities by key the easy way</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792681</link><pubDate>Sat, 20 Jun 2009 05:43:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792681</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;Sometimes rather than writing this: var customer = ctx.Customers.First(c =&amp;amp;gt; c.ID == 5); You would&lt;/p&gt;
</description></item><item><title>Tip 24 – How to get the ObjectContext from an Entity</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792694</link><pubDate>Sat, 20 Jun 2009 05:49:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792694</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;Customers often ask how to get from an Entity back to the ObjectContext . Now generally we don’t recommend&lt;/p&gt;
</description></item><item><title>Tip 23 – How to fake Enums in EF 4</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792703</link><pubDate>Sat, 20 Jun 2009 05:56:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792703</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;As of right now Enums are not in EF4. Now we will be listening to your feedback about Beta1, and making&lt;/p&gt;
</description></item><item><title>Tip 22 - How to make Include really Include</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792721</link><pubDate>Sat, 20 Jun 2009 06:02:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792721</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;This is 22nd post in my ongoing series of Entity Framework Tips . If you want to do eager loading with&lt;/p&gt;
</description></item><item><title>Tip 21 – How to use the Single() operator – EF 4.0 only</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792727</link><pubDate>Sat, 20 Jun 2009 06:04:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792727</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;This is 21st post in my ongoing series of Entity Framework Tips , and the first that is specific to EF&lt;/p&gt;
</description></item><item><title>Tip 20 – How to deal with Fixed Length Keys</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792744</link><pubDate>Sat, 20 Jun 2009 06:10:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792744</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;This is 20th post in my ongoing series of Entity Framework Tips . Fixed Length Field Padding: If you&lt;/p&gt;
</description></item><item><title>Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792758</link><pubDate>Sat, 20 Jun 2009 06:15:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792758</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;This is the 19th post in my ongoing series of Entity Framework Tips . Background: If you have a table&lt;/p&gt;
</description></item><item><title>Tip 13 - How to Attach an Entity the easy way</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9792846</link><pubDate>Sat, 20 Jun 2009 07:26:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792846</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;The Problem: In some of earlier tips we talked about using Attach to load things into the ObjectContext&lt;/p&gt;
</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9850797</link><pubDate>Tue, 28 Jul 2009 11:47:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9850797</guid><dc:creator>Jesús Jiménez</dc:creator><description>&lt;p&gt;Congrats for this serie of posts Alex, they're great!&lt;/p&gt;
&lt;p&gt;I'm learing a lot with them, keep up the good work.&lt;/p&gt;
&lt;p&gt;Regards!&lt;/p&gt;</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9898651</link><pubDate>Wed, 23 Sep 2009 23:35:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9898651</guid><dc:creator>Osa</dc:creator><description>&lt;p&gt;To Aleem question: This is a solution starter. I did not cover navigations because of infinite recursion.&lt;/p&gt;
&lt;p&gt;public static string SerilaizeJSON(this EntityObject entity)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;StringBuilder sb = new StringBuilder(200);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;PropertyInfo[] propertyInfos = entity.GetType().GetProperties();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Type scalarPropertyType = typeof(EdmScalarPropertyAttribute);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;sb.Append(&amp;quot;{\&amp;quot;&amp;quot; + entity.GetType().Name + &amp;quot;\&amp;quot; : {&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;foreach (PropertyInfo p in propertyInfos) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (p.GetCustomAttributes(scalarPropertyType, false).Count &amp;gt; 0) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sb.AppendFormat(&amp;quot;\&amp;quot;{0}\&amp;quot; : \&amp;quot;{1}\&amp;quot;,&amp;quot;, p.Name, p.GetValue(entity, null));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (propertyInfos.Count &amp;gt; 0) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sb.Remove(sb.Length - 1, 1);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;sb.Append(&amp;quot;}}&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return sb.ToString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;</description></item><item><title>re: Entity Framework Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9905400</link><pubDate>Fri, 09 Oct 2009 15:00:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9905400</guid><dc:creator>Yash</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;We have a User entity type mapped to a table named tbl_user_profile. This has fields like firstname, lastname, etc. Another entity type Coordinator is mapped to tbl_coordinator. The table has a FK name user_id pointing to the tbl_user_profile table. All coordinators are users but the reverse is not true. The framework we use, expects all fields in the same class and hence, we want all fields of the coordinator in the coordinator entity type. We will not use inheritance because we use RIA domain services which does not support inheritance. &lt;/p&gt;
&lt;p&gt;To achieve this, we added the tbl_user_profile table in the mapping for Coordinator entity type. While doing this we get following error :&lt;/p&gt;
&lt;p&gt;&amp;quot;EntitySets 'Users' and 'Coordinators' are both mapped to table 'tbl_user_profile'. Their Primary Keys may collide&amp;quot;.&lt;/p&gt;
&lt;p&gt;Error	1	Error 3033: Problem in Mapping Fragment starting at line 2016: EntitySets 'Users' and 'Coordinators' are both mapped to table 'tbl_user_profile'. Their Primary Keys may collide.&lt;/p&gt;
&lt;p&gt;	E:\EDMX\EntityModel.edmx	2017	15	MyTrial.Data&lt;/p&gt;
&lt;p&gt;&amp;quot;User&amp;quot; has entityset named &amp;quot;Users&amp;quot; and &amp;quot;Coordinator&amp;quot; has entityset named &amp;quot;Coordinators&amp;quot;. &lt;/p&gt;
&lt;p&gt;What is the cause and solution to this problem. Any way to work around it?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Yash&lt;/p&gt;</description></item><item><title>re: Entity Framework &amp; Data Service Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9928341</link><pubDate>Wed, 25 Nov 2009 02:22:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9928341</guid><dc:creator>Blogger</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The tip 35 link is broken. &lt;/p&gt;
&lt;p&gt;However google &amp;lt; OfTypeOnly reveals this url which works.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/alexj/archive/2009/09/17/tip-35-how-to-write-oftypeonly-tentity.aspx"&gt;http://blogs.msdn.com/alexj/archive/2009/09/17/tip-35-how-to-write-oftypeonly-tentity.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Can you please correct.&lt;/p&gt;</description></item><item><title>re: Entity Framework &amp; Data Service Tips</title><link>http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx#9928428</link><pubDate>Wed, 25 Nov 2009 07:48:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9928428</guid><dc:creator>AlexJ</dc:creator><description>&lt;p&gt;@Blogger,&lt;/p&gt;
&lt;p&gt;Thanks for the catch!&lt;/p&gt;
&lt;p&gt;Fixed!&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
</description></item></channel></rss>