<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Akash Kava &#187; C#</title>
	<atom:link href="http://akashkava.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://akashkava.com/blog</link>
	<description>Founder of NeuroSpeech</description>
	<lastBuildDate>Mon, 19 Sep 2011 16:21:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Detach Entities Recursively and Attach to New DomainContext in Entity Framework and RIA Services</title>
		<link>http://akashkava.com/blog/379/detach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services/</link>
		<comments>http://akashkava.com/blog/379/detach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 08:03:00 +0000</pubDate>
		<dc:creator>Akash Kava</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[EF4]]></category>
		<category><![CDATA[Entity]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[RIA Services]]></category>
		<category><![CDATA[Services]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://akashkava.com/blog/?p=379</guid>
		<description><![CDATA[Entity Framework and RIA Services do not allow Entities to be added/removed from cross containers. In order to move Entity from one DomainContext/ObjectContext to another DomainContext/ObjectContext, we need to recursively detach the object graph and attach it to other DomainContext/ObjectContext. Object Graph Single entity can be easily detached and attached from EntitySet where it belongs, [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://akashkava.com/blog/379/detach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Detach+Entities+Recursively+and+Attach+to+New+DomainContext+in+Entity+Framework+and+RIA+Services";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fakashkava.com%2Fblog%2F379%2Fdetach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fakashkava.com%2Fblog%2F379%2Fdetach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Entity Framework and RIA Services do not allow Entities to be added/removed from cross containers. In order to move Entity from one DomainContext/ObjectContext to another DomainContext/ObjectContext, we need to recursively detach the object graph and attach it to other DomainContext/ObjectContext.</p>
<h2>Object Graph</h2>
<p>Single entity can be easily detached and attached from EntitySet where it belongs, but the problem comes when navigation properties of entity are not empty. Entity object along with navigation properties is called Object Graph because if you notice, navigation property’s navigation property will contain reference to same entity and that will result in endless recursive code for Detach/Attach.</p>
<h2>An entity with the same identity already exists in the EntitySet</h2>
<p>When you try to detach entity from old DomainContext/ObjectContext and attach it to DomainContext/ObjectContext, it may give you an error that entity with same identity already exists and it will throw an exception. In this case we will just simply reuse the existing entity instead of attaching the entity we have.</p>
<h2>Entity cannot be attached to this EntityContainer because it is already attached to another EntityContainer</h2>
<p>In case of ObjectGraph, your root level entity is already detached, but navigation properties are not detached, and while you try to attach your root level entity, it will throw same error for entities that exist in navigation properties.&#160; Because detach method does not recursively detach every entity from navigation properties.</p>
<h2>Attach/Detach Extension Methods</h2>
<p>Finally after brainstorming little, I made following class that will allow you to recursively detach/attach object graphs from DomainContext. You can replace DomainContext to ObjectContext to use it inside Entity Framework.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1357a75c-1e52-42b7-8917-81a262c881ac" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px">DomainContext Extensions</div>
<div style="background: #ddd; overflow: auto">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px; white-space: nowrap">
<li><span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#808080">///</span><span style="color:#008000"> DomainContext Extensions</span></li>
<li><span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3"><span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">DomainContextExtensions</span> {</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> Recursively Attaches entity loaded from Other DomainContext to </span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> current specified DomainContext</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;context&quot;&gt;</span><span style="color:#008000">DomainContext where entity will be attached</span><span style="color:#808080">&lt;/param&gt;</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;entity&quot;&gt;</span><span style="color:#008000">Entity loaded from other DomainContext</span><span style="color:#808080">&lt;/param&gt;</span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;returns&gt;&lt;/returns&gt;</span></li>
<li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#2b91af">Entity</span> Attach(<span style="color:#0000ff">this</span> <span style="color:#2b91af">DomainContext</span> context, <span style="color:#2b91af">Entity</span> entity)</li>
<li style="background: #f3f3f3">    {</li>
<li>        <span style="color:#0000ff">if</span> (entity == <span style="color:#0000ff">null</span> || entity.EntityState != <span style="color:#2b91af">EntityState</span>.Detached)</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">return</span> entity;</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">        <span style="color:#2b91af">Entity</span> newEntity = entity;</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">        <span style="color:#2b91af">Entity</span>[] list = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Entity</span>[] { entity };</li>
<li>        <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">Entity</span> c <span style="color:#0000ff">in</span> context.EntityContainer.LoadEntities(list, </li>
<li style="background: #f3f3f3">            <span style="color:#2b91af">LoadBehavior</span>.MergeIntoCurrent))</li>
<li>        {</li>
<li style="background: #f3f3f3">            newEntity = c;</li>
<li>            <span style="color:#0000ff">break</span>;</li>
<li style="background: #f3f3f3">        }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">        <span style="color:#008000">// recursively attach all entities..</span></li>
<li>        <span style="color:#2b91af">Type</span> entityType = <span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">Entity</span>);</li>
<li style="background: #f3f3f3">&nbsp;</li>
<li>        <span style="color:#008000">// get all navigation properties&#8230;</span></li>
<li style="background: #f3f3f3">        <span style="color:#2b91af">Type</span> type = entity.GetType();</li>
<li>        <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> item <span style="color:#0000ff">in</span> type.GetProperties())</li>
<li style="background: #f3f3f3">        {</li>
<li>            <span style="color:#0000ff">if</span> (entityType.IsAssignableFrom(item.PropertyType))</li>
<li style="background: #f3f3f3">            {</li>
<li>                <span style="color:#2b91af">Entity</span> navEntity = Attach(context, item.GetValue(entity, <span style="color:#0000ff">null</span>) </li>
<li style="background: #f3f3f3">                    <span style="color:#0000ff">as</span> <span style="color:#2b91af">Entity</span>);</li>
<li>                item.SetValue(newEntity,navEntity, <span style="color:#0000ff">null</span>);</li>
<li style="background: #f3f3f3">                <span style="color:#0000ff">continue</span>;</li>
<li>            }</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">if</span> (item.PropertyType.Name.StartsWith(<span style="color:#a31515">&quot;EntityCollection&quot;</span>))</li>
<li>            {</li>
<li style="background: #f3f3f3">                <span style="color:#2b91af">IEnumerable</span> coll = item.GetValue(entity, <span style="color:#0000ff">null</span>) <span style="color:#0000ff">as</span> <span style="color:#2b91af">IEnumerable</span>;</li>
<li>                <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Entity</span>&gt; newList = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Entity</span>&gt;();</li>
<li style="background: #f3f3f3">                <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">Entity</span> child <span style="color:#0000ff">in</span> coll)</li>
<li>                {</li>
<li style="background: #f3f3f3">                    newList.Add(Attach(context, child));</li>
<li>                }</li>
<li style="background: #f3f3f3">                <span style="color:#0000ff">dynamic</span> dcoll = item.GetValue(newEntity,<span style="color:#0000ff">null</span>);</li>
<li>                <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">dynamic</span> child <span style="color:#0000ff">in</span> newList)</li>
<li style="background: #f3f3f3">                {</li>
<li>                    dcoll.Add(child);</li>
<li style="background: #f3f3f3">                }</li>
<li>            }</li>
<li style="background: #f3f3f3">        }</li>
<li>        <span style="color:#0000ff">return</span> newEntity;</li>
<li style="background: #f3f3f3">    }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> Recursively detaches entities from DomainContext, this </span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> method detaches every navigation properties</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> of current Entity as well.</span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span></li>
<li>    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;context&quot;&gt;&lt;/param&gt;</span></li>
<li style="background: #f3f3f3">    <span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;</span></li>
<li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> Detach(<span style="color:#0000ff">this</span> <span style="color:#2b91af">DomainContext</span> context, <span style="color:#2b91af">Entity</span> entity)</li>
<li style="background: #f3f3f3">    {</li>
<li>        <span style="color:#0000ff">if</span> (entity == <span style="color:#0000ff">null</span> || entity.EntityState == <span style="color:#2b91af">EntityState</span>.Detached)</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">return</span>;</li>
<li>        <span style="color:#2b91af">EntitySet</span> nes = context.EntityContainer.GetEntitySet(entity.GetType());</li>
<li style="background: #f3f3f3">        nes.Detach(entity);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">        <span style="color:#2b91af">Type</span> entityType = <span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">Entity</span>);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">        <span style="color:#008000">// get all navigation properties&#8230;</span></li>
<li>        <span style="color:#2b91af">Type</span> type = entity.GetType();</li>
<li style="background: #f3f3f3">        <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> item <span style="color:#0000ff">in</span> type.GetProperties())</li>
<li>        {</li>
<li style="background: #f3f3f3">            <span style="color:#0000ff">if</span> (entityType.IsAssignableFrom(item.PropertyType))</li>
<li>            {</li>
<li style="background: #f3f3f3">                Detach(context,item.GetValue(entity, <span style="color:#0000ff">null</span>) <span style="color:#0000ff">as</span> <span style="color:#2b91af">Entity</span>);</li>
<li>                <span style="color:#0000ff">continue</span>;</li>
<li style="background: #f3f3f3">            }</li>
<li>            <span style="color:#0000ff">if</span> (item.PropertyType.Name.StartsWith(<span style="color:#a31515">&quot;EntityCollection&quot;</span>))</li>
<li style="background: #f3f3f3">            {</li>
<li>                <span style="color:#2b91af">IEnumerable</span> coll = item.GetValue(entity, <span style="color:#0000ff">null</span>) <span style="color:#0000ff">as</span> <span style="color:#2b91af">IEnumerable</span>;</li>
<li style="background: #f3f3f3">                <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">Entity</span> child <span style="color:#0000ff">in</span> coll)</li>
<li>                {</li>
<li style="background: #f3f3f3">                    Detach(context,child);</li>
<li>                }</li>
<li style="background: #f3f3f3">            }</li>
<li>        }</li>
<li style="background: #f3f3f3">    }</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3">}</li>
</ol></div>
</p></div>
</p></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fakashkava.com%2Fblog%2F379%2Fdetach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services%2F&amp;title=Detach%20Entities%20Recursively%20and%20Attach%20to%20New%20DomainContext%20in%20Entity%20Framework%20and%20RIA%20Services"><img src="http://akashkava.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://akashkava.com/blog/379/detach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using SQL Assembly to replace T-SQL Business Logic</title>
		<link>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/</link>
		<comments>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 09:00:32 +0000</pubDate>
		<dc:creator>Akash Kava</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://akashkava.com/blog/?p=259</guid>
		<description><![CDATA[SQL Assembly brings power of CLR (Common language runtime) inside SQL Database Stored Procedure which can overcome T-SQL programming pains of Intellisense, Debugging, Logging and Versioning. Traditional Problems with T-SQL and Stored Procedures I never liked programming in T-SQL, for various reasons listed below, No Intellisense, of course it has been introduced SQL 2008 onwards, [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Using+SQL+Assembly+to+replace+T-SQL+Business+Logic";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>SQL Assembly brings power of CLR (Common language runtime) inside SQL Database Stored Procedure which can overcome T-SQL programming pains of Intellisense, Debugging, Logging and Versioning.</p>
<h2>Traditional Problems with T-SQL and Stored Procedures</h2>
<p>I never liked programming in T-SQL, for various reasons listed below,</p>
<ol>
<li><font color="#333333">No Intellisense, of course it has been introduced SQL 2008 onwards, but still its far behind what we get in Visual Studio.</font> </li>
<li><font color="#333333">Source Code Control, although scripts can be added to source code control, usually its unawareness and unwillingness of Database Administrator / Designer to use it correctly and mess things up.</font> </li>
<li><font color="#333333">Less human readable, T-SQL is very difficult to understand if its not documented well.</font> </li>
<li><font color="#333333">Source code is open, it can be modified, so in short Database Administrator has complete access to alter business logic.</font> </li>
<li><font color="#333333">Very less reusable, unless you do copy paste, no way to standardize the development.</font> </li>
<li><font color="#333333">No support for advanced visual tools to design T-SQL Stored Procedures.</font> </li>
<li><font color="#333333">Deployment is difficult, minor alternation can cause problems.</font> </li>
<li><font color="#333333">Most of business logic involves, loops, if-else, switch statements, and it takes 10 times more code to execute such logic in T-SQL then any other programming language in world.</font> </li>
<li><font color="#333333">You need multiple language talent, one with either VB.NET/C# and T-SQL.</font> </li>
<li>Difficult to Log information.</li>
</ol>
<h2>Introduction of SQL Assembly</h2>
<p>Since SQL 2005, introduction of SQL Assembly has been of great importance, however not much of people still use it, because of unawareness. But SQL Assembly can be modularized and reused, and it exists as simple CLR assembly inside database. </p>
<p>And following are benefits of SQL Assembly, besides few listed in <a href="http://msdn.microsoft.com/en-us/library/k2e1fb36(v=VS.100).aspx" target="_blank">MSDN here</a>.</p>
<ol>
<li><font color="#333333">LINQ in CLR Stored Procedure.</font> </li>
<li><font color="#333333">Better Source Code control over Visual C#/Visual Basic.NET project through Team System.</font> </li>
<li><font color="#333333">Intellisense.</font> </li>
<li><font color="#333333">Closed source, its safe.</font> </li>
<li><font color="#333333">Highly secure as Database Administrator can not alter business logic.</font> </li>
<li><font color="#333333">C# and VB.NET along with LINQ are more human readable, understanding and reusing code can be very easy.</font> </li>
<li><font color="#333333">Support for advanced documentation in comments.</font> </li>
<li><font color="#333333">Visual Studio Model designer can be used.</font> </li>
<li>Reuse your Business Logic for other database vendor products, e.g. Oracle, MySQL (Explained below). </li>
<li>You can do advanced logging with log-4-net or custom logging.</li>
</ol>
<h2>Small Example of How to use LINQ in SQL Assembly</h2>
<ol>
<li>Create a simple Visual C# Class Library Project. </li>
<li>Add reference of “System.Data” Assembly. </li>
<li>Create new ADO.NET Entity Model, and you can create it from Database or empty model, the way you want to develop your project. </li>
<li>Add few entities, I am going to demonstrate entity “Cart” which is a saved shopping cart for the Customer. </li>
</ol>
<h2><a href="http://akashkava.com/blog/wp-content/uploads/2010/04/CartDemo.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CartDemo" border="0" alt="CartDemo" src="http://akashkava.com/blog/wp-content/uploads/2010/04/CartDemo_thumb.png" width="389" height="206" /></a>Create a Stored Procedure Class</h2>
<p>After creating model, lets create a class called “ShoppingCart” and add a static method as shown below.</p>
<blockquote><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">ShoppingCart
</span>{

    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Creates new Cart if none exists and returns the Cart ID
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name=&quot;CustomerID&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span>[<span style="color: #2b91af">SqlProcedure</span>]
    <span style="color: blue">public static </span><span style="color: #2b91af">SqlInt64 </span>GetSavedCartID(<span style="color: #2b91af">SqlInt64 </span>CustomerID)
    {

        <span style="color: green">// Set context connection=true
        </span><span style="color: #2b91af">ShoppingCartModelContainer </span>context =
            <span style="color: blue">new </span><span style="color: #2b91af">ShoppingCartModelContainer</span>(<span style="color: #a31515">&quot;context connection=true&quot;</span>);

        <span style="color: green">// Query for exisitng cart
        </span><span style="color: #2b91af">Cart </span>c = context.Carts.FirstOrDefault(
                        t =&gt; t.CustomerID == CustomerID.Value);

        <span style="color: blue">if </span>(c == <span style="color: blue">null</span>) { 

            <span style="color: green">// create new Cart
            </span>c = <span style="color: #2b91af">Cart</span>.CreateCart(0, CustomerID.Value, <span style="color: #2b91af">DateTime</span>.Now);
            context.AddToCarts(c);
            context.SaveChanges();
        }

        <span style="color: green">// return the Cart ID
        </span><span style="color: blue">return new </span><span style="color: #2b91af">SqlInt64</span>(c.CartdID);

    }

}</pre>
</blockquote>
<p align="justify">This code demonstrates how easy it is to write SQL Assembly, now here is the ultimate benefit of everything.</p>
<h2>Ultimate Benefit</h2>
<p align="justify">You can use your Business Logic in multiple vendor databases, like for example for Oracle and MySQL, you can reuse your SQL Assembly and just change your connection string. You can reuse your business logic in your front end code with simple wrapper.</p>
<p align="justify">And with SQL Server, you can reuse this assembly inside SQL Database.</p>
<p align="justify"><a href="http://akashkava.com/blog/wp-content/uploads/2010/04/CodeSharing.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CodeSharing" border="0" alt="CodeSharing" src="http://akashkava.com/blog/wp-content/uploads/2010/04/CodeSharing_thumb.png" width="644" height="200" /></a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F&amp;title=Using%20SQL%20Assembly%20to%20replace%20T-SQL%20Business%20Logic"><img src="http://akashkava.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Optimize Windows Vista Machine for Faster Development (VC#,Web and VC++)</title>
		<link>http://akashkava.com/blog/141/optimize-windows-vista-machine-for-faster-development-vcweb-and-vc/</link>
		<comments>http://akashkava.com/blog/141/optimize-windows-vista-machine-for-faster-development-vcweb-and-vc/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 10:22:56 +0000</pubDate>
		<dc:creator>Akash Kava</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fast]]></category>
		<category><![CDATA[Machine]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://akashkava.com/blog/?p=141</guid>
		<description><![CDATA[This document only refers to optimization of Vista Machine for faster development only intended for the ones who are doing development on C#, ASP.NET and Optional VC++, no VB.NET. Installation of Limited Visual Studio 2008 We probably dont need Compact Frameworks and VB.NET and other tools, unnecessary installing them doesnt waste much of Hard disk [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://akashkava.com/blog/141/optimize-windows-vista-machine-for-faster-development-vcweb-and-vc/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Optimize+Windows+Vista+Machine+for+Faster+Development+%28VC%23%2CWeb+and+VC%2B%2B%29";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fakashkava.com%2Fblog%2F141%2Foptimize-windows-vista-machine-for-faster-development-vcweb-and-vc%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fakashkava.com%2Fblog%2F141%2Foptimize-windows-vista-machine-for-faster-development-vcweb-and-vc%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This document only refers to optimization of Vista Machine for faster development only intended for the ones who are doing development on C#, ASP.NET and Optional VC++, no VB.NET.</p>
<h1>Installation of Limited Visual Studio 2008</h1>
<p>We probably dont need Compact Frameworks and VB.NET and other tools, unnecessary installing them doesnt waste much of Hard disk space but what happens that your registry grows too big as you install more number of items. Registry is very costly database, and its accessed very regularly, more items in registry, slower and slower the PC. So here is list of items you should install.</p>
<div id="attachment_163" class="wp-caption alignnone" style="width: 559px"><a href="http://akashkava.com/blog/wp-content/uploads/2009/08/RequiredItems.jpg" target="_blank"><img class="size-full wp-image-163 " title="Visual Studio Items" src="http://akashkava.com/blog/wp-content/uploads/2009/08/RequiredItems.jpg" alt="Visual Studio Items" width="549" height="421" /></a><p class="wp-caption-text">Visual Studio Items</p></div>
<h1>Disable High End Graphics, Faster Performance</h1>
<div id="attachment_164" class="wp-caption alignnone" style="width: 513px"><a href="http://akashkava.com/blog/wp-content/uploads/2009/08/Disable-High-End-Graphics.png" target="_blank"><img class="size-full wp-image-164  " title="Disable High End Graphics" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Disable-High-End-Graphics.png" alt="Disable High End Graphics" width="503" height="312" /></a><p class="wp-caption-text">Disable High End Graphics</p></div>
<h1>Disable System Restore Points</h1>
<p>Now, System Restore Points are good, but usually on development machine, they run very slow, and they never do any good if system crashes, the restore time is as big as and as problematic as to reinstall OS.</p>
<div id="attachment_168" class="wp-caption alignnone" style="width: 498px"><a href="http://akashkava.com/blog/wp-content/uploads/2009/08/Disable-System-Restore-Points.png" target="_blank"><img class="size-full wp-image-168 " title="Disable System Restore Points" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Disable-System-Restore-Points.png" alt="Disable System Restore Points" width="488" height="368" /></a><p class="wp-caption-text">Disable System Restore Points</p></div>
<h1>Increase Paging Size</h1>
<p>Increase it as much as you can in different drive but the acceptable range is 2GB to 4GB only.</p>
<div id="attachment_170" class="wp-caption alignnone" style="width: 501px"><a href="http://akashkava.com/blog/wp-content/uploads/2009/08/Increase-Paging-Size.png" target="_blank"><img class="size-full wp-image-170  " title="Increase Paging Size" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Increase-Paging-Size.png" alt="Increase Paging Size" width="491" height="356" /></a><p class="wp-caption-text">Increase Paging Size</p></div>
<h1>Remove Unnecessary Vista Items as shown below</h1>
<div id="attachment_171" class="wp-caption alignnone" style="width: 510px"><a href="http://akashkava.com/blog/wp-content/uploads/2009/08/Programs-and-Features.png" target="_blank"><img class="size-full wp-image-171  " title="Programs and Features" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Programs-and-Features.png" alt="Remove Unnecessary Programs and Features" width="500" height="367" /></a><p class="wp-caption-text">Remove Unnecessary Programs and Features</p></div>
<h1>Setup Taskbar and Start Menu</h1>
<div id="attachment_169" class="wp-caption alignnone" style="width: 434px"><img class="size-full wp-image-169" title="Taskbar and Startmenu Properties Setup 1" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Taskbar-and-Startmenu-Properties-Setup-1.png" alt="Taskbar and Startmenu Properties" width="424" height="469" /><p class="wp-caption-text">Taskbar and Startmenu Properties</p></div>
<h1>Expand Items for Quicker Use</h1>
<div id="attachment_172" class="wp-caption alignnone" style="width: 564px"><img class="size-full wp-image-172" title="Expand Control Panel" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Expand-Control-Panel.png" alt="Expand Control Panel" width="554" height="598" /><p class="wp-caption-text">Expand Control Panel</p></div>
<div id="attachment_173" class="wp-caption alignnone" style="width: 564px"><img class="size-full wp-image-173" title="Expand Documents" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Expand-Documents.png" alt="Expand Documents" width="554" height="598" /><p class="wp-caption-text">Expand Documents</p></div>
<div id="attachment_174" class="wp-caption alignnone" style="width: 564px"><img class="size-full wp-image-174" title="Expand Admin Tools" src="http://akashkava.com/blog/wp-content/uploads/2009/08/Expand-Admin-Tools.png" alt="Expand Admin Tools" width="554" height="598" /><p class="wp-caption-text">Expand Admin Tools</p></div>
<p style="text-align: center;">
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fakashkava.com%2Fblog%2F141%2Foptimize-windows-vista-machine-for-faster-development-vcweb-and-vc%2F&amp;title=Optimize%20Windows%20Vista%20Machine%20for%20Faster%20Development%20%28VC%23%2CWeb%20and%20VC%2B%2B%29"><img src="http://akashkava.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://akashkava.com/blog/141/optimize-windows-vista-machine-for-faster-development-vcweb-and-vc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

