<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Morshed Anwar&#039;s Blog</title>
	<atom:link href="http://morshedanwar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://morshedanwar.wordpress.com</link>
	<description>My webLog About Me mine and Myself</description>
	<lastBuildDate>Fri, 06 Jan 2012 11:22:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='morshedanwar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/004bf1bec31663165e84d9f11fe65810?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Morshed Anwar&#039;s Blog</title>
		<link>http://morshedanwar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://morshedanwar.wordpress.com/osd.xml" title="Morshed Anwar&#039;s Blog" />
	<atom:link rel='hub' href='http://morshedanwar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Implementing repository Pattern With EF4 POCO support</title>
		<link>http://morshedanwar.wordpress.com/2011/06/30/implementing-repository-pattern-with-ef4-poco-support/</link>
		<comments>http://morshedanwar.wordpress.com/2011/06/30/implementing-repository-pattern-with-ef4-poco-support/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 10:52:00 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[EF4]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[POCO]]></category>
		<category><![CDATA[repository pattern]]></category>

		<guid isPermaLink="false">https://morshedanwar.wordpress.com/2011/06/30/implementing-repository-pattern-with-ef4-poco-support/</guid>
		<description><![CDATA[&#160; About two years ago have written a post and an article in codeproject about the repository pattern implementation with EF V1. After that I got a lot of requests in my articles to make it compatible for EF4 POCO support. So here I am going to make that. In this implementation of repository, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=154&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="csharpcode">
<pre>&nbsp;</pre>
</div>
<p style="margin:0 0 10pt;" class="MsoNormal"><span style="line-height:115%;font-family:'Courier New';color:black;font-size:10pt;" lang="EN">About two years ago have written a </span><span style="line-height:115%;font-family:'Courier New';font-size:10pt;" lang="EN"><a href="http://morshedanwar.wordpress.com/2009/06/10/implementing-repository-pattern-with-entity-framework/"><font color="#0000ff">post</font></a><span style="color:black;"> and an </span><a href="http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx"><font color="#0000ff">article</font></a><span style="color:black;"> in <a href="http://www.codeproject.com/" rel="tag" target="_blank">codeproject</a> about the repository pattern implementation with EF V1. After that I got a lot of requests in my articles to make it compatible for EF4 POCO support. So here I am going to make that. In this implementation of repository, I am going to add some new methods which has helped me to regularly while working with EF4 POCO support. </span></span><span style="font-family:'Courier New';" lang="EN"></span></p>
<p><span style="font-family:'Courier New';color:black;font-size:10pt;" lang="EN">Hope it will help you-</span></p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Repository</span>&lt;E, C&gt; : <span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;, <span style="color:#2b91af;">IDisposable
    </span><span style="color:blue;">where </span>E : <span style="color:#2b91af;">BaseDataContract
    </span><span style="color:blue;">where </span>C : <span style="color:#2b91af;">BaseContext
</span>{
    <span style="color:blue;">private </span>C _ctx;

    <span style="color:blue;">private string </span>_KeyProperty = <span style="color:#a31515;">"ID"</span>;

    <span style="color:blue;">public string </span>KeyProperty
    {
        <span style="color:blue;">get
        </span>{
            <span style="color:blue;">return </span>_KeyProperty;
        }
        <span style="color:blue;">set
        </span>{
            _KeyProperty = <span style="color:blue;">value</span>;
        }
    }

    <span style="color:blue;">public </span>C Session
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>_ctx; }
        <span style="color:blue;">set </span>{ _ctx = <span style="color:blue;">value</span>; }
    }

    <span style="color:blue;">public </span>Repository(C session)
    {
        _ctx = session;
    }

    <span style="color:blue;">public </span>Repository(C session, <span style="color:blue;">string </span>keyProperty) : <span style="color:blue;">this</span>(session)
    {
        _KeyProperty = keyProperty;
    }

    <span style="color:blue;">#region </span>IRepository&lt;E,C&gt; Members

    <span style="color:blue;">public int </span>Save()
    {
        <span style="color:blue;">return </span>_ctx.SaveChanges();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. In Version
    </span><span style="color:gray;">/// </span><span style="color:green;">2.0 this pluralizing behaviour will change and this
    </span><span style="color:gray;">/// </span><span style="color:green;">method overload should be used only if Developers
    </span><span style="color:gray;">/// </span><span style="color:green;">change EntitySet names from the default name generated.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName)
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ entitySetName + <span style="color:#a31515;">"]"</span>);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery()
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:blue;">return
            </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ entitySetName + <span style="color:#a31515;">"]"</span>)
            .Where(where.EvalPredicate);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:blue;">return
            </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>)
            .Where(where.EvalPredicate);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query Entity with Paging
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="maximumRows"&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="startRowIndex"&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>).Skip&lt;E&gt;(startRowIndex).Take(maximumRows);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query Entity in sorted Order
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="sortExpression"&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="ErrorCode"&gt;</span><span style="color:green;">custom Error Message</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">object</span>&gt;&gt; sortExpression)
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>== sortExpression)
        {
            <span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery();
        }
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery().OrderBy&lt;E, <span style="color:blue;">object</span>&gt;(sortExpression);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query All Entity in sorted Order with Paging support
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="sortExpression"&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="maximumRows"&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="startRowIndex"&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection Of entites</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">object</span>&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">if </span>(sortExpression == <span style="color:blue;">null</span>)
        {
            <span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery(maximumRows, startRowIndex);
        }
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery(sortExpression).Skip&lt;E&gt;(startRowIndex).Take(maximumRows);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. In Version
    </span><span style="color:gray;">/// </span><span style="color:green;">2.0 this pluralizing behaviour will change and this
    </span><span style="color:gray;">/// </span><span style="color:green;">method overload should be used only if Developers
    </span><span style="color:gray;">/// </span><span style="color:green;">change EntitySet names from the default name generated.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName)
    {
        <span style="color:blue;">return </span>DoQuery(entitySetName);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll()
    {
        <span style="color:blue;">try
        </span>{
            <span style="color:blue;">return </span>DoQuery(); <span style="color:green;">//_ctx.CreateQuery&lt;E&gt;("[" + typeof(E).Name + "]");
        </span>}
        <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception</span>)
        {
            <span style="color:blue;">throw</span>;
        }
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return </span>DoQuery(entitySetName, where);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return </span>DoQuery(where);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity with Paging
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="maximumRows"&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="startRowIndex"&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">return </span>DoQuery(maximumRows, startRowIndex);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity in sorted Order
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="sortExpression"&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="ErrorCode"&gt;</span><span style="color:green;">custom Error Message</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">object</span>&gt;&gt; sortExpression)
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>== sortExpression)
        {
            <span style="color:blue;">return </span>DoQuery(sortExpression);
        }
        <span style="color:blue;">return </span>DoQuery(sortExpression);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity in sorted Order with Paging support
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="sortExpression"&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="maximumRows"&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="startRowIndex"&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection Of entites</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; SelectAll(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">object</span>&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">if </span>(sortExpression == <span style="color:blue;">null</span>)
        {
            <span style="color:blue;">return </span>DoQuery(maximumRows, startRowIndex);
        }
        <span style="color:blue;">return </span>DoQuery(sortExpression, maximumRows, startRowIndex);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get Entity By Primary Key
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;</span><span style="color:green;">Entity Type</span><span style="color:gray;">&lt;/typeparam&gt;
    /// &lt;param name="Key"&gt;</span><span style="color:green;">Primary Key Value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">return entity</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span>E SelectByKey(<span style="color:blue;">string </span>Key)
    {
        _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">true</span>;
        <span style="color:green;">// First we define the parameter that we are going to use the clause.
        </span><span style="color:blue;">var </span>xParam = <span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(E), <span style="color:blue;">typeof</span>(E).Name);
        <span style="color:#2b91af;">MemberExpression </span>leftExpr = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, <span style="color:blue;">this</span>._KeyProperty);
        <span style="color:#2b91af;">Expression </span>rightExpr = <span style="color:#2b91af;">Expression</span>.Constant(Key);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExpr = <span style="color:#2b91af;">MemberExpression</span>.Equal(leftExpr, rightExpr);
        <span style="color:green;">//Create Lambda Expression for the selection
        </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; lambdaExpr = <span style="color:#2b91af;">Expression</span>.Lambda&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt;(binaryExpr, <span style="color:blue;">new </span><span style="color:#2b91af;">ParameterExpression</span>[] { xParam });
        <span style="color:green;">//Searching ....
        </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; resultCollection = ((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).SelectAll(<span style="color:blue;">new </span><span style="color:#2b91af;">Specification</span>&lt;E&gt;(lambdaExpr));
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= resultCollection &amp;&amp; resultCollection.Count() &gt; 0)
        {
            <span style="color:green;">//return valid single result
            </span><span style="color:blue;">return </span>resultCollection.First&lt;E&gt;();
        }<span style="color:green;">//end if
        </span><span style="color:blue;">return null</span>;
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Check if value of specific field is already exist
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;param name="fieldName"&gt;</span><span style="color:green;">name of the Field</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="fieldValue"&gt;</span><span style="color:green;">Field value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name="key"&gt;</span><span style="color:green;">Primary key value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">True or False</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public bool </span>TrySameValueExist(<span style="color:blue;">string </span>fieldName, <span style="color:blue;">object </span>fieldValue, <span style="color:blue;">string </span>key)
    {
        <span style="color:green;">// First we define the parameter that we are going to use the clause.
        </span><span style="color:blue;">var </span>xParam = <span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(E), <span style="color:blue;">typeof</span>(E).Name);
        <span style="color:#2b91af;">MemberExpression </span>leftExprFieldCheck = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, fieldName);
        <span style="color:#2b91af;">Expression </span>rightExprFieldCheck = <span style="color:#2b91af;">Expression</span>.Constant(fieldValue);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExprFieldCheck = <span style="color:#2b91af;">MemberExpression</span>.Equal(leftExprFieldCheck, rightExprFieldCheck);

        <span style="color:#2b91af;">MemberExpression </span>leftExprKeyCheck = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, <span style="color:blue;">this</span>._KeyProperty);
        <span style="color:#2b91af;">Expression </span>rightExprKeyCheck = <span style="color:#2b91af;">Expression</span>.Constant(key);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExprKeyCheck = <span style="color:#2b91af;">MemberExpression</span>.NotEqual(leftExprKeyCheck, rightExprKeyCheck);
        <span style="color:#2b91af;">BinaryExpression </span>finalBinaryExpr = <span style="color:#2b91af;">Expression</span>.And(binaryExprFieldCheck, binaryExprKeyCheck);

        <span style="color:green;">//Create Lambda Expression for the selection
        </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; lambdaExpr = <span style="color:#2b91af;">Expression</span>.Lambda&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt;(finalBinaryExpr, <span style="color:blue;">new </span><span style="color:#2b91af;">ParameterExpression</span>[] { xParam });
        <span style="color:green;">//Searching ....
        </span><span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).TryEntity(<span style="color:blue;">new </span><span style="color:#2b91af;">Specification</span>&lt;E&gt;(lambdaExpr));
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Check if Entities exist with Condition
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="selectExpression"&gt;</span><span style="color:green;">Selection Condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">True or False</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public bool </span>TryEntity(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec)
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>).Any&lt;E&gt;(selectSpec.EvalPredicate);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get Count of all records
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">count of all records</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public int </span>GetCount()
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>).Count();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get count of selection
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;</span><span style="color:green;">Selection Condition</span><span style="color:gray;">&lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">count of selection</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public int </span>GetCount(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec)
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">"[" </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">"]"</span>)
            .Where(selectSpec.EvalPredicate).Count();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Delete data from context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>Delete(E entity)
    {
        <span style="color:green;">//object deletedObject;
        //_ctx.TryGetObjectByKey(entity.EntityKey, out deletedObject);
        </span>_ctx.DeleteObject(entity);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Delete data from context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>Delete(<span style="color:blue;">object </span>entity)
    {
        <span style="color:green;">//object deletedObject;
        //_ctx.TryGetObjectByKey(((EntityObject)entity).EntityKey, out deletedObject);
        </span>_ctx.DeleteObject(entity);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Insert new data into context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>Add(E entity)
    {
        _ctx.AddObject(entity.GetType().Name, entity);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Insert if new otherwise attach data into context. Plus,
    </span><span style="color:gray;">/// </span><span style="color:green;">returns a list of changed property name and value (in
    </span><span style="color:gray;">/// </span><span style="color:green;">this format--&gt; "propName|originalValue|newValue")- if any.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt; AddOrAttach(E entity, <span style="color:#2b91af;">EntityKey </span>key = <span style="color:blue;">null</span>)
    {
        <span style="color:blue;">try
        </span>{
            _ctx.ContextOptions.LazyLoadingEnabled = <span style="color:blue;">false</span>;

            <span style="color:#2b91af;">ObjectStateEntry </span>entry = <span style="color:blue;">null</span>;
            <span style="color:green;">//Add the object graph into the context
            </span><span style="color:blue;">if </span>(<span style="color:blue;">null </span>== key )
            {
                <span style="color:blue;">var </span>entitySet = _ctx.GetEntitySet(entity.GetType());
                key = _ctx.CreateEntityKey(entitySet.Name, entity);
            }

            <span style="color:blue;">if</span>( <span style="color:blue;">null </span>!= key )
                _ctx.ObjectStateManager.TryGetObjectStateEntry(key, <span style="color:blue;">out </span>entry);
            <span style="color:blue;">if </span>(entry == <span style="color:blue;">null</span>)
            {
                _ctx.AddObject(<span style="color:blue;">typeof</span>(E).Name, entity);
                _ctx.ObjectStateManager.ChangeObjectState(entity, <span style="color:#2b91af;">StateHelpers</span>.GetEquivelantEntityState(entity.ObjectState));
            }
            <span style="color:blue;">else
            </span>{
                _ctx.ApplyCurrentValues(<span style="color:blue;">typeof</span>(E).Name, entity);
            }
            <span style="color:green;">//get the changed column values (needed for audit trail)
            </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt; changedCols = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;();
            <span style="color:blue;">var </span>ose = (entry != <span style="color:blue;">null</span>)? entry : _ctx.ObjectStateManager.GetObjectStateEntry(entity);
            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>propName <span style="color:blue;">in </span>ose.GetModifiedProperties())
            {
                <span style="color:blue;">string </span>pNameValue = propName;
                pNameValue += <span style="color:#a31515;">"|" </span>+ ose.OriginalValues[propName] + <span style="color:#a31515;">"|" </span>+ ose.CurrentValues[propName];
                changedCols.Add(pNameValue);
            }

            <span style="color:blue;">return </span>changedCols;
        }
        <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception</span>)
        {
            <span style="color:blue;">throw</span>;
        }
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Start Transaction
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">DbTransaction </span>BeginTransaction()
    {
        <span style="color:blue;">if </span>(_ctx.Connection.State != <span style="color:#2b91af;">ConnectionState</span>.Open)
        {
            _ctx.Connection.Open();
        }
        <span style="color:blue;">return </span>_ctx.Connection.BeginTransaction();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Change state of Navigation Object(s)
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt;
    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
    /// &lt;param name="context"&gt;&lt;/param&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    /// &lt;param name="state"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>ChangeStateOfNavigationObject(E entity, <span style="color:#2b91af;">State </span>state)
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= entity)
        {
            <span style="color:blue;">var </span>navigationProperties = _ctx.GetNavigationProperty&lt;C, E&gt;();
            <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= navigationProperties)
            {
                <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>navigationProperty <span style="color:blue;">in </span>navigationProperties)
                {
                    <span style="color:blue;">var </span>info = entity.GetType().GetProperty(navigationProperty.Name);
                    <span style="color:blue;">var </span>value = info.GetValue(entity, <span style="color:blue;">null</span>);

                    <span style="color:blue;">if </span>(value != <span style="color:blue;">null</span>)
                    {

                        <span style="color:blue;">if </span>(((<span style="color:#2b91af;">AssociationType</span>)navigationProperty.RelationshipType).IsForeignKey
                            &amp;&amp;
                            navigationProperty.FromEndMember.RelationshipMultiplicity !=
                            <span style="color:#2b91af;">RelationshipMultiplicity</span>.Many)
                        {
                            <span style="color:#2b91af;">Type </span>t = value.GetType();
                            <span style="color:#2b91af;">Type </span>baseType;
                            <span style="color:blue;">if </span>(!t.IsGenericType)
                            {
                                baseType = t.BaseType;

                                <span style="color:blue;">if </span>(baseType.BaseType.Equals(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">BaseDataContract</span>)))
                                {
                                    <span style="color:#2b91af;">MethodInfo </span>method = <span style="color:blue;">this</span>.GetType().GetMethod(<span style="color:#a31515;">"ChangeObjectStatetoAttach"</span>);
                                    <span style="color:#2b91af;">MethodInfo </span>generic = method.MakeGenericMethod(baseType);
                                    generic.Invoke(<span style="color:blue;">this</span>, <span style="color:blue;">new object</span>[] { value, state });
                                }
                            }
                            <span style="color:blue;">else
                            </span>{
                                <span style="color:#2b91af;">Type</span>[] genericBases = t.GetGenericArguments();
                                <span style="color:green;">// Q&amp;D: pick the first one
                                </span>baseType = genericBases[0];

                                <span style="color:blue;">if </span>(baseType.BaseType.Equals(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">BaseDataContract</span>)))
                                {
                                    <span style="color:#2b91af;">MethodInfo </span>method =
                                        <span style="color:blue;">this</span>.GetType().GetMethod(<span style="color:#a31515;">"ChangeCollectionObjectStatetoAttach"</span>);
                                    <span style="color:#2b91af;">MethodInfo </span>generic = method.MakeGenericMethod(baseType);
                                    generic.Invoke(<span style="color:blue;">this</span>, <span style="color:blue;">new object</span>[] { value, state });
                                }
                            }
                        }
                        <span style="color:blue;">else
                        </span>{
                            info.SetValue(entity, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>);
                        }
                    }<span style="color:green;">//end if
                </span>}
            }
        }
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">change state of an single object
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name="entity"&gt;&lt;/param&gt;
    /// &lt;param name="state"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>ChangeObjectStatetoAttach&lt;T&gt;(T entity, <span style="color:#2b91af;">State </span>state) <span style="color:blue;">where </span>T : <span style="color:#2b91af;">BaseDataContract
    </span>{
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= entity)
        {
            <span style="color:blue;">var </span>dataContract = entity <span style="color:blue;">as </span><span style="color:#2b91af;">BaseDataContract</span>;
            dataContract.ObjectState = state;
            <span style="color:#2b91af;">EntityKey </span>entityKey = _ctx.CreateEntityKey(
                _ctx.GetEntitySet(dataContract.GetType()).Name, dataContract);
            <span style="color:#2b91af;">ObjectStateEntry </span>storeEntity;
            <span style="color:blue;">if </span>(_ctx.ObjectStateManager.TryGetObjectStateEntry(entityKey, <span style="color:blue;">out </span>storeEntity))
            {
                _ctx.ApplyCurrentValues(_ctx.GetEntitySet(<span style="color:blue;">typeof</span>(T)).Name, entity);
                <span style="color:green;">//(storeEntity as T).ObjectState = State.Unchanged;
            </span>}
            <span style="color:blue;">else
            </span>{
                _ctx.AddObject(_ctx.GetEntitySet(dataContract.GetType()).Name, dataContract);
            }
            _ctx.ObjectStateManager.ChangeObjectState(dataContract,
                                                         <span style="color:#2b91af;">StateHelpers</span>.
                                                             GetEquivelantEntityState
                                                             (dataContract.ObjectState));
        }
    }
    <span style="color:gray;">/// &lt;summary&gt;
    ///
    /// &lt;/summary&gt;
    /// &lt;param name="entities"&gt;&lt;/param&gt;
    /// &lt;param name="state"&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>ChangeCollectionObjectStatetoAttach&lt;T&gt;(<span style="color:#2b91af;">FixupCollection</span>&lt;T&gt; entities, <span style="color:#2b91af;">State </span>state)
       <span style="color:blue;">where </span>T : <span style="color:#2b91af;">BaseDataContract
    </span>{
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= entities)
        {
            <span style="color:blue;">for </span>(<span style="color:blue;">int </span>i = 0; i &lt; entities.Count; i++)
            {
                ChangeObjectStatetoAttach&lt;T&gt;(entities[i], state);
            }
        }
    }

    <span style="color:blue;">#endregion

    #region </span>IDisposable Members

    <span style="color:blue;">public void </span>Dispose()
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= _ctx)
        {
            <span style="color:blue;">if </span>(_ctx.Connection.State == <span style="color:#2b91af;">ConnectionState</span>.Open)
                _ctx.Connection.Close();
            _ctx.Dispose();
        }
    }

    <span style="color:blue;">#endregion

</span>}
</pre>
<p>&nbsp;</p>
<pre><font size="2"><span style="color:black;" lang="EN">To work with some bulk operation and getting related entities on runtime,&nbsp; I have added some extension methods to find out related navigation info –</span></font><span style="font-family:'Courier New';color:#606060;" lang="EN"><font size="3">&nbsp;</font></span></pre>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:94.95%;font-family:'Courier New', courier, monospace;direction:ltr;height:200px;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> List&lt;NavigationProperty&gt; GetNavigationProperty&lt;TObjectContext, T&gt;(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>         <span style="color:#0000ff;">this</span> ObjectContext context)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>         <span style="color:#0000ff;">where</span> TObjectContext : ObjectContext</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>          {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>              var containerName = context.DefaultContainerName;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>              var model = DataSpace.CSpace;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>              var workspace = context.MetadataWorkspace;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>              var container = workspace.GetEntityContainer(containerName, model);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>              EntitySetBase entitySet = context.GetEntitySet(<span style="color:#0000ff;">typeof</span> (T));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>              <span style="color:#0000ff;">if</span> (entitySet == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>                  <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>              <span style="color:#008000;">//materialize nav props for testing</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>              var navigationProps = entitySet.ElementType.Members</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>                  .Where(m =&gt; m.BuiltInTypeKind == BuiltInTypeKind.NavigationProperty</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>                  )</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>                  .Cast&lt;NavigationProperty&gt;()</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>                  .ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>             <span style="color:#0000ff;">return</span> navigationProps;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>          }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>   </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> EntitySetBase GetEntitySet(<span style="color:#0000ff;">this</span> ObjectContext context, Type entityType)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>             <span style="color:#0000ff;">if</span> (context == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>                 <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> ArgumentNullException(<span style="color:#006080;">"context"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum30">  30:</span>             <span style="color:#0000ff;">if</span> (entityType == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum31">  31:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum32">  32:</span>                 <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> ArgumentNullException(<span style="color:#006080;">"entityType"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum33">  33:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum34">  34:</span>             EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum35">  35:</span>             <span style="color:#0000ff;">if</span> (container == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum36">  36:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum37">  37:</span>                 <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum38">  38:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum39">  39:</span>             EntitySetBase entitySet = container.BaseEntitySets.Where(item =&gt; item.ElementType.Name.Equals(entityType.Name))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum40">  40:</span>                                                               .FirstOrDefault();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum41">  41:</span>             <span style="color:#0000ff;">return</span> entitySet;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum42">  42:</span>         }</pre>
<p><!--CRLF--></div>
</div>
<p>&nbsp;</p>
<p style="background:white;margin:0 0 10pt;" class="MsoNormal"><span style="line-height:115%;font-family:'Courier New';color:black;font-size:10pt;" lang="EN">Also some methods in context which I got from my very favorite </span><span style="line-height:115%;font-family:'Courier New';color:black;font-size:9pt;" lang="EN"><a href="https://msmvps.com/blogs/matthieu/default.aspx"><font color="#0000ff">Matthieu MEZIL</font></a> </span><span style="line-height:115%;font-family:'Courier New';color:black;font-size:10pt;" lang="EN">post of <a href="http://msmvps.com/blogs/matthieu/archive/2010/05/21/bulk-delete-v3.aspx"><font color="#0000ff">bulk delete operation</font></a> -</span></p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:95.23%;font-family:'Courier New', courier, monospace;direction:ltr;height:210px;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> BaseContext : ObjectContext, IObjectContextWithBulkOperations</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>         <span style="color:#cc6633;">#region</span> Constructors</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>         <span style="color:#0000ff;">public</span> BaseContext(<span style="color:#0000ff;">string</span> connectionString, <span style="color:#0000ff;">string</span> containerName)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>             : <span style="color:#0000ff;">base</span>(connectionString, containerName)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>             <span style="color:#0000ff;">this</span>.ContextOptions.LazyLoadingEnabled = <span style="color:#0000ff;">false</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>         <span style="color:#0000ff;">public</span> BaseContext(EntityConnection connection, <span style="color:#0000ff;">string</span> containerName)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>             : <span style="color:#0000ff;">base</span>(connection, containerName)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>             <span style="color:#0000ff;">this</span>.ContextOptions.LazyLoadingEnabled = <span style="color:#0000ff;">false</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>     </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>         <span style="color:#cc6633;">#endregion</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>         <span style="color:#cc6633;">#region</span> IContext Members</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Save()</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>             <span style="color:#0000ff;">string</span> validationErrors;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>             <span style="color:#0000ff;">if</span> (ValidateBeforeSave(<span style="color:#0000ff;">out</span> validationErrors))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>                 <span style="color:#008000;">//StateFixup();</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span>                 SaveChanges();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>                 <span style="color:#0000ff;">return</span> <span style="color:#006080;">""</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum30">  30:</span>             <span style="color:#0000ff;">return</span> <span style="color:#006080;">"Data Not Saved due to Validation Errors: "</span> + validationErrors;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum31">  31:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum32">  32:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum33">  33:</span>         <span style="color:#0000ff;">public</span> IEnumerable&lt;T&gt; ManagedEntities&lt;T&gt;()</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum34">  34:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum35">  35:</span>             var oses =</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum36">  36:</span>                 ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified |</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum37">  37:</span>                                                          EntityState.Unchanged | EntityState.Detached);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum38">  38:</span>             <span style="color:#0000ff;">return</span> oses.Where(entry =&gt; entry.Entity <span style="color:#0000ff;">is</span> T)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum39">  39:</span>                        .Select(entry =&gt; (T)entry.Entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum40">  40:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum41">  41:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum42">  42:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> ValidateBeforeSave(<span style="color:#0000ff;">out</span> <span style="color:#0000ff;">string</span> validationErrors)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum43">  43:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum44">  44:</span>             <span style="color:#0000ff;">bool</span> isvalid = <span style="color:#0000ff;">true</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum45">  45:</span>             validationErrors = <span style="color:#006080;">""</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum46">  46:</span>             <span style="color:#0000ff;">return</span> isvalid;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum47">  47:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum48">  48:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum49">  49:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> ChangeState&lt;T&gt;(State state, T entity) <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum50">  50:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum51">  51:</span>             ObjectStateManager.ChangeObjectState(entity, StateHelpers.GetEquivelantEntityState(state));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum52">  52:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum53">  53:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum54">  54:</span>         <span style="color:#cc6633;">#endregion</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum55">  55:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum56">  56:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateTrackedEntity&lt;T&gt;(T modifiedEntity) <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum57">  57:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum58">  58:</span>             var set = <span style="color:#0000ff;">this</span>.CreateObjectSet&lt;T&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum59">  59:</span>             set.ApplyCurrentValues(modifiedEntity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum60">  60:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum61">  61:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum62">  62:</span>         <span style="color:#cc6633;">#region</span> IObjectContextWithBulkOperations Member</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum63">  63:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum64">  64:</span>         <span style="color:#0000ff;">private</span> List&lt;Action&gt; _bulkDeletedActions;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum65">  65:</span>         <span style="color:#0000ff;">private</span> List&lt;Action&gt; BulkDeletedActions</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum66">  66:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum67">  67:</span>             get</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum68">  68:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum69">  69:</span>                 <span style="color:#0000ff;">if</span> (_bulkDeletedActions == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum70">  70:</span>                     _bulkDeletedActions = <span style="color:#0000ff;">new</span> List&lt;Action&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum71">  71:</span>                 <span style="color:#0000ff;">return</span> _bulkDeletedActions;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum72">  72:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum73">  73:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum74">  74:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum75">  75:</span>         <span style="color:#0000ff;">private</span> List&lt;<span style="color:#0000ff;">object</span>&gt; _bulkDeletedEntities;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum76">  76:</span>         <span style="color:#0000ff;">public</span> List&lt;<span style="color:#0000ff;">object</span>&gt; BulkDeletedEntities</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum77">  77:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum78">  78:</span>             get</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum79">  79:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum80">  80:</span>                 <span style="color:#0000ff;">if</span> (_bulkDeletedEntities == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum81">  81:</span>                     _bulkDeletedEntities = <span style="color:#0000ff;">new</span> List&lt;<span style="color:#0000ff;">object</span>&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum82">  82:</span>                 <span style="color:#0000ff;">return</span> _bulkDeletedEntities;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum83">  83:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum84">  84:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum85">  85:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum86">  86:</span>         <span style="color:#0000ff;">private</span> Dictionary&lt;Type, List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;&gt; _bulkDeletedFuncs;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum87">  87:</span>         <span style="color:#0000ff;">public</span> Dictionary&lt;Type, List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;&gt; BulkDeletedFuncs</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum88">  88:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum89">  89:</span>             get</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum90">  90:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum91">  91:</span>                 <span style="color:#0000ff;">if</span> (_bulkDeletedFuncs == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum92">  92:</span>                     _bulkDeletedFuncs = <span style="color:#0000ff;">new</span> Dictionary&lt;Type, List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum93">  93:</span>                 <span style="color:#0000ff;">return</span> _bulkDeletedFuncs;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum94">  94:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum95">  95:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum96">  96:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum97">  97:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Delete&lt;TBase, T&gt;(ObjectSet&lt;TBase&gt; entitySet, Expression&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt; predicate)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum98">  98:</span>             <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span>, TBase</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum99">  99:</span>             <span style="color:#0000ff;">where</span> TBase : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum100"> 100:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum101"> 101:</span>             Delete&lt;TBase, T&gt;(entitySet, predicate, <span style="color:#0000ff;">true</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum102"> 102:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum103"> 103:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> DeleteLoadedEntities&lt;TBase, T&gt;(ObjectSet&lt;TBase&gt; entitySet, Expression&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt; predicate)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum104"> 104:</span>             <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span>, TBase</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum105"> 105:</span>             <span style="color:#0000ff;">where</span> TBase : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum106"> 106:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum107"> 107:</span>             <span style="color:#0000ff;">if</span> ((predicate = CalculatePredicate(entitySet, predicate)) != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum108"> 108:</span>                 Delete&lt;TBase, T&gt;(entitySet, predicate, <span style="color:#0000ff;">false</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum109"> 109:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum110"> 110:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum111"> 111:</span>         <span style="color:#0000ff;">private</span> Expression&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt; CalculatePredicate&lt;TBase, T&gt;(ObjectSet&lt;TBase&gt; entitySet, Expression&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt; oldPredicate)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum112"> 112:</span>             <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span>, TBase</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum113"> 113:</span>             <span style="color:#0000ff;">where</span> TBase : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum114"> 114:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum115"> 115:</span>             IEnumerable&lt;PropertyInfo&gt; keyMembers = entitySet.EntitySet.ElementType.KeyMembers.Select(km =&gt; <span style="color:#0000ff;">typeof</span>(T).GetProperty(km.Name)).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum116"> 116:</span>             IEnumerable&lt;T&gt; entitiesEnumerable = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum117"> 117:</span>                                                 .Select(ose =&gt; ose.Entity)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum118"> 118:</span>                                                 .OfType&lt;T&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum119"> 119:</span>             ParameterExpression parameter = oldPredicate.Parameters.Single();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum120"> 120:</span>             <span style="color:#0000ff;">if</span> (!entitiesEnumerable.Any())</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum121"> 121:</span>                 <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum122"> 122:</span>             <span style="color:#0000ff;">return</span> Expression.Lambda&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt;(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum123"> 123:</span>                 Expression.AndAlso(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum124"> 124:</span>                     oldPredicate.Body,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum125"> 125:</span>                     entitiesEnumerable.Select(e =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum126"> 126:</span>                         keyMembers.Select(km =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum127"> 127:</span>                             Expression.Equal(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum128"> 128:</span>                                 Expression.MakeMemberAccess(parameter, km),</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum129"> 129:</span>                                 Expression.Constant(km.GetValue(e, <span style="color:#0000ff;">null</span>))))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum130"> 130:</span>                         .Aggregate((accumulate, clause) =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum131"> 131:</span>                             Expression.AndAlso(accumulate, clause)))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum132"> 132:</span>                     .Aggregate((accumulate, clause) =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum133"> 133:</span>                         Expression.OrElse(accumulate, clause)))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum134"> 134:</span>                 , oldPredicate.Parameters);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum135"> 135:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum136"> 136:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum137"> 137:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> Delete&lt;TBase, T&gt;(ObjectSet&lt;TBase&gt; entitySet, Expression&lt;Func&lt;T, <span style="color:#0000ff;">bool</span>&gt;&gt; predicate, <span style="color:#0000ff;">bool</span> propagateToFutureEntities)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum138"> 138:</span>             <span style="color:#0000ff;">where</span> TBase : <span style="color:#0000ff;">class</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum139"> 139:</span>             <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span>, TBase</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum140"> 140:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum141"> 141:</span>             ObjectQuery&lt;T&gt; objectQuery = (ObjectQuery&lt;T&gt;)entitySet.OfType&lt;T&gt;().Where(predicate);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum142"> 142:</span>             <span style="color:#0000ff;">string</span> selectSQLQuery = objectQuery.ToTraceString();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum143"> 143:</span>             List&lt;KeyValuePair&lt;<span style="color:#0000ff;">string</span>, List&lt;<span style="color:#0000ff;">string</span>&gt;&gt;&gt; froms = <span style="color:#0000ff;">new</span> List&lt;KeyValuePair&lt;<span style="color:#0000ff;">string</span>, List&lt;<span style="color:#0000ff;">string</span>&gt;&gt;&gt;();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum144"> 144:</span>             Match fromMatch = Regex.Match(entitySet.OfType&lt;T&gt;().ToTraceString(), <span style="color:#006080;">"(FROM|JOIN)[ ]+((\\[[^\\]]+\\]).)*\\[([^\\]]+)\\]"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum145"> 145:</span>             List&lt;AssociationType&gt; ssdlAsscociations = MetadataWorkspace.GetItems(DataSpace.SSpace).OfType&lt;AssociationType&gt;().ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum146"> 146:</span>             <span style="color:#0000ff;">string</span> firstFrom = <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum147"> 147:</span>             <span style="color:#0000ff;">while</span> (fromMatch.Success)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum148"> 148:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum149"> 149:</span>                 <span style="color:#0000ff;">string</span> fromValue = fromMatch.Groups[4].Value;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum150"> 150:</span>                 <span style="color:#0000ff;">if</span> (Regex.IsMatch(selectSQLQuery, <span style="color:#0000ff;">string</span>.Format(<span style="color:#006080;">"(FROM|JOIN)[ ]+((\\[[^\\]]+\\]).)*\\[{0}\\]"</span>, fromValue)))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum151"> 151:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum152"> 152:</span>                     var index = (from ssdlAssociation <span style="color:#0000ff;">in</span> ssdlAsscociations</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum153"> 153:</span>                                  <span style="color:#0000ff;">where</span> ssdlAssociation.ReferentialConstraints.Any(rc =&gt; fromValue == rc.ToProperties.First().DeclaringType.Name)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum154"> 154:</span>                                  from table <span style="color:#0000ff;">in</span> froms.Select((f, i) =&gt; <span style="color:#0000ff;">new</span> { Table = f, Index = i })</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum155"> 155:</span>                                  <span style="color:#0000ff;">where</span> ssdlAssociation.ReferentialConstraints.Any(rc =&gt; table.Table.Key == rc.FromProperties.First().DeclaringType.Name)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum156"> 156:</span>                                  orderby table.Index</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum157"> 157:</span>                                  select <span style="color:#0000ff;">new</span> { Index = table.Index, SSDLAssociation = ssdlAssociation, FKs = table.Table }).FirstOrDefault();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum158"> 158:</span>                     <span style="color:#0000ff;">if</span> (index != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum159"> 159:</span>                         froms.Insert(index.Index, <span style="color:#0000ff;">new</span> KeyValuePair&lt;<span style="color:#0000ff;">string</span>, List&lt;<span style="color:#0000ff;">string</span>&gt;&gt;(fromValue, (from fk <span style="color:#0000ff;">in</span> index.FKs.Value</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum160"> 160:</span>                                                                                                      let referentailConstraint = index.SSDLAssociation.ReferentialConstraints.First(rc =&gt; index.FKs.Key == rc.FromProperties.First().DeclaringType.Name)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum161"> 161:</span>                                                                                                      select referentailConstraint.ToProperties.ElementAt(referentailConstraint.FromProperties.Select((p, pIndex) =&gt; <span style="color:#0000ff;">new</span> { p.Name, Index = pIndex }).First(p =&gt; p.Name == fk).Index).Name).ToList()));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum162"> 162:</span>                     <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum163"> 163:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum164"> 164:</span>                         <span style="color:#0000ff;">if</span> (firstFrom == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum165"> 165:</span>                             firstFrom = fromValue;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum166"> 166:</span>                         froms.Add(<span style="color:#0000ff;">new</span> KeyValuePair&lt;<span style="color:#0000ff;">string</span>, List&lt;<span style="color:#0000ff;">string</span>&gt;&gt;(fromValue, MetadataWorkspace.GetItems(DataSpace.SSpace).OfType&lt;EntityType&gt;().First(et =&gt; et.Name == fromValue).KeyMembers.Select(km =&gt; km.Name).ToList()));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum167"> 167:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum168"> 168:</span>                 }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum169"> 169:</span>                 fromMatch = fromMatch.NextMatch();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum170"> 170:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum171"> 171:</span>             StringBuilder delete = <span style="color:#0000ff;">new</span> StringBuilder();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum172"> 172:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum173"> 173:</span>             <span style="color:#0000ff;">string</span> selectSQLQueryWithoutSelect = selectSQLQuery.Substring(selectSQLQuery.IndexOf(<span style="color:#006080;">"FROM"</span>));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum174"> 174:</span>             IEnumerator&lt;EdmMember&gt; keyMembersEnumerator = <span style="color:#0000ff;">null</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum175"> 175:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum176"> 176:</span>             <span style="color:#0000ff;">if</span> (froms.Count &gt; 1)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum177"> 177:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum178"> 178:</span>                 delete.Append(<span style="color:#006080;">"declare @DeleteIds table ("</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum179"> 179:</span>                 StringBuilder keys = <span style="color:#0000ff;">new</span> StringBuilder();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum180"> 180:</span>                 keyMembersEnumerator = MetadataWorkspace.GetItems(DataSpace.SSpace).OfType&lt;EntityType&gt;().</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum181"> 181:</span>                     First(et =&gt; et.Name == firstFrom).KeyMembers.ToList().GetEnumerator();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum182"> 182:</span>                 keyMembersEnumerator.MoveNext();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum183"> 183:</span>                 <span style="color:#0000ff;">for</span> (; ; )</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum184"> 184:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum185"> 185:</span>                     <span style="color:#0000ff;">string</span> keyName = keyMembersEnumerator.Current.Name;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum186"> 186:</span>                     keys.Append(keyName);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum187"> 187:</span>                     delete.Append(keyName);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum188"> 188:</span>                     delete.Append(<span style="color:#006080;">" "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum189"> 189:</span>                     delete.Append(keyMembersEnumerator.Current.TypeUsage.EdmType.Name);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum190"> 190:</span>                     Facet maxLength = keyMembersEnumerator.Current.TypeUsage.Facets.FirstOrDefault(f =&gt; f.Name == <span style="color:#006080;">"MaxLength"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum191"> 191:</span>                     <span style="color:#0000ff;">if</span> (maxLength != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum192"> 192:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum193"> 193:</span>                         delete.Append(<span style="color:#006080;">"("</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum194"> 194:</span>                         delete.Append(maxLength.Value);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum195"> 195:</span>                         delete.Append(<span style="color:#006080;">")"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum196"> 196:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum197"> 197:</span>                     <span style="color:#0000ff;">if</span> (keyMembersEnumerator.MoveNext())</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum198"> 198:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum199"> 199:</span>                         keys.Append(<span style="color:#006080;">", "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum200"> 200:</span>                         delete.Append(<span style="color:#006080;">", "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum201"> 201:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum202"> 202:</span>                     <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum203"> 203:</span>                         <span style="color:#0000ff;">break</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum204"> 204:</span>                 }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum205"> 205:</span>                 delete.Append(<span style="color:#006080;">");\n"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum206"> 206:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum207"> 207:</span>                 delete.Append(<span style="color:#006080;">"INSERT INTO @DeleteIds SELECT "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum208"> 208:</span>                 delete.Append(keys.ToString());</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum209"> 209:</span>                 delete.Append(<span style="color:#006080;">" "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum210"> 210:</span>                 delete.Append(selectSQLQueryWithoutSelect.Replace(<span style="color:#006080;">"@p__linq__"</span>, <span style="color:#006080;">"@p"</span>));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum211"> 211:</span>                 delete.Append(<span style="color:#006080;">";\n"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum212"> 212:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum213"> 213:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum214"> 214:</span>             <span style="color:#0000ff;">foreach</span> (KeyValuePair&lt;<span style="color:#0000ff;">string</span>, List&lt;<span style="color:#0000ff;">string</span>&gt;&gt; from <span style="color:#0000ff;">in</span> froms)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum215"> 215:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum216"> 216:</span>                 delete.Append(<span style="color:#006080;">"DELETE FROM ["</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum217"> 217:</span>                 delete.Append(from.Key);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum218"> 218:</span>                 delete.Append(<span style="color:#006080;">"] FROM "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum219"> 219:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum220"> 220:</span>                 <span style="color:#0000ff;">if</span> (froms.Count &gt; 1)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum221"> 221:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum222"> 222:</span>                     delete.Append(<span style="color:#006080;">"["</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum223"> 223:</span>                     delete.Append(from.Key);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum224"> 224:</span>                     delete.Append(<span style="color:#006080;">"]"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum225"> 225:</span>                     delete.Append(<span style="color:#006080;">"INNER JOIN @deleteIds D ON "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum226"> 226:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum227"> 227:</span>                     keyMembersEnumerator.Reset();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum228"> 228:</span>                     keyMembersEnumerator.MoveNext();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum229"> 229:</span>                     <span style="color:#0000ff;">int</span> index = 0;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum230"> 230:</span>                     <span style="color:#0000ff;">for</span> (; ; )</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum231"> 231:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum232"> 232:</span>                         delete.Append(<span style="color:#006080;">"["</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum233"> 233:</span>                         delete.Append(from.Key);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum234"> 234:</span>                         delete.Append(<span style="color:#006080;">"]."</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum235"> 235:</span>                         delete.Append(from.Value[index++]);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum236"> 236:</span>                         delete.Append(<span style="color:#006080;">" = D."</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum237"> 237:</span>                         delete.Append(keyMembersEnumerator.Current);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum238"> 238:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum239"> 239:</span>                         <span style="color:#0000ff;">if</span> (keyMembersEnumerator.MoveNext())</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum240"> 240:</span>                             delete.Append(<span style="color:#006080;">" AND "</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum241"> 241:</span>                         <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum242"> 242:</span>                             <span style="color:#0000ff;">break</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum243"> 243:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum244"> 244:</span>                 }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum245"> 245:</span>                 <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum246"> 246:</span>                     delete.Append(selectSQLQueryWithoutSelect.Substring(4).TrimStart());</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum247"> 247:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum248"> 248:</span>                 delete.Append(<span style="color:#006080;">";\n"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum249"> 249:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum250"> 250:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum251"> 251:</span>             BulkDeletedActions.Add(() =&gt; ExecuteStoreCommand(delete.ToString(), objectQuery.Parameters.Select(p =&gt; p.Value).ToArray()));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum252"> 252:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum253"> 253:</span>             Func&lt;T, <span style="color:#0000ff;">bool</span>&gt; predicateCompiled = predicate.Compile();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum254"> 254:</span>             Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt; predicateCompiledObject = o =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum255"> 255:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum256"> 256:</span>                 T t = o <span style="color:#0000ff;">as</span> T;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum257"> 257:</span>                 <span style="color:#0000ff;">if</span> (t == <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum258"> 258:</span>                     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum259"> 259:</span>                 <span style="color:#0000ff;">return</span> predicateCompiled(t);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum260"> 260:</span>             };</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum261"> 261:</span>             <span style="color:#0000ff;">if</span> (propagateToFutureEntities)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum262"> 262:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum263"> 263:</span>                 List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt; bulkDeletedFuncs;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum264"> 264:</span>                 <span style="color:#0000ff;">if</span> (BulkDeletedFuncs.TryGetValue(<span style="color:#0000ff;">typeof</span>(TBase), <span style="color:#0000ff;">out</span> bulkDeletedFuncs))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum265"> 265:</span>                     bulkDeletedFuncs.Add(predicateCompiledObject);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum266"> 266:</span>                 <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum267"> 267:</span>                     BulkDeletedFuncs.Add(<span style="color:#0000ff;">typeof</span>(TBase), <span style="color:#0000ff;">new</span> List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;() { predicateCompiledObject });</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum268"> 268:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum269"> 269:</span>             EntityType entityType = MetadataWorkspace.GetItems(DataSpace.CSpace).OfType&lt;EntityType&gt;().First(et =&gt; et.Name == <span style="color:#0000ff;">typeof</span>(T).Name);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum270"> 270:</span>             var oneToOneSubEntityTypes = (from np <span style="color:#0000ff;">in</span> entityType.NavigationProperties</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum271"> 271:</span>                                           <span style="color:#0000ff;">where</span> np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One &amp;&amp; np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum272"> 272:</span>                                           let otherEntityType = np.ToEndMember.GetEntityType()</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum273"> 273:</span>                                           let otherNavigationProperty = otherEntityType.NavigationProperties.FirstOrDefault(otherNP =&gt; otherNP.RelationshipType == np.RelationshipType)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum274"> 274:</span>                                           select <span style="color:#0000ff;">new</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum275"> 275:</span>                                           {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum276"> 276:</span>                                               EntityType = otherEntityType,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum277"> 277:</span>                                               ClrType = <span style="color:#0000ff;">typeof</span>(T).GetProperty(np.Name).PropertyType,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum278"> 278:</span>                                               OtherNavigationPropertyName = otherNavigationProperty == <span style="color:#0000ff;">null</span> ? <span style="color:#0000ff;">null</span> : otherNavigationProperty.Name,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum279"> 279:</span>                                               ReferencialConstraint = ((AssociationType)np.RelationshipType).ReferentialConstraints.FirstOrDefault()</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum280"> 280:</span>                                           }).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum281"> 281:</span>             <span style="color:#0000ff;">foreach</span> (var subEntityTypeLoop <span style="color:#0000ff;">in</span> oneToOneSubEntityTypes)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum282"> 282:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum283"> 283:</span>                 var subEntityType = subEntityTypeLoop;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum284"> 284:</span>                 <span style="color:#0000ff;">if</span> (subEntityType.OtherNavigationPropertyName != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum285"> 285:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum286"> 286:</span>                     List&lt;<span style="color:#0000ff;">string</span>&gt; entityTypeKeys, subEntityTypeKeys;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum287"> 287:</span>                     <span style="color:#0000ff;">if</span> (subEntityType.ReferencialConstraint.FromProperties.First().DeclaringType == entityType)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum288"> 288:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum289"> 289:</span>                         entityTypeKeys = subEntityType.ReferencialConstraint.FromProperties.Select(p =&gt; p.Name).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum290"> 290:</span>                         subEntityTypeKeys = subEntityType.ReferencialConstraint.ToProperties.Select(p =&gt; p.Name).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum291"> 291:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum292"> 292:</span>                     <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum293"> 293:</span>                     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum294"> 294:</span>                         entityTypeKeys = subEntityType.ReferencialConstraint.ToProperties.Select(p =&gt; p.Name).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum295"> 295:</span>                         subEntityTypeKeys = subEntityType.ReferencialConstraint.FromProperties.Select(p =&gt; p.Name).ToList();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum296"> 296:</span>                     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum297"> 297:</span>                     ParameterExpression entityParameter = Expression.Parameter(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>), <span style="color:#006080;">"entity"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum298"> 298:</span>                     ParameterExpression subEntityParameter = Expression.Parameter(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>), <span style="color:#006080;">"subEntity"</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum299"> 299:</span>                     Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt; associateToBulkEntities =</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum300"> 300:</span>                         Expression.Lambda&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum301"> 301:</span>                             entityTypeKeys.Select((entityTypeKey, keyIndex) =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum302"> 302:</span>                                 Expression.Equal(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum303"> 303:</span>                                     Expression.MakeMemberAccess(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum304"> 304:</span>                                         Expression.Convert(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum305"> 305:</span>                                             subEntityParameter,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum306"> 306:</span>                                             subEntityType.ClrType),</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum307"> 307:</span>                                         subEntityType.ClrType.GetProperty(subEntityTypeKeys[keyIndex])),</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum308"> 308:</span>                                     Expression.MakeMemberAccess(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum309"> 309:</span>                                         Expression.Convert(</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum310"> 310:</span>                                             entityParameter,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum311"> 311:</span>                                             <span style="color:#0000ff;">typeof</span>(T)),</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum312"> 312:</span>                                         <span style="color:#0000ff;">typeof</span>(T).GetProperty(entityTypeKey)))).</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum313"> 313:</span>                             Aggregate((accumulate, keyPredicate) =&gt; Expression.AndAlso(accumulate, keyPredicate)),</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum314"> 314:</span>                             entityParameter,</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum315"> 315:</span>                             subEntityParameter).</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum316"> 316:</span>                             Compile();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum317"> 317:</span>                     Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt; npPredicate = subE =&gt; BulkDeletedEntities.OfType&lt;T&gt;().Any(e =&gt; associateToBulkEntities(e, subE));</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum318"> 318:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum319"> 319:</span>                     List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt; bulkDeletedFuncs;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum320"> 320:</span>                     <span style="color:#0000ff;">if</span> (BulkDeletedFuncs.TryGetValue(subEntityType.ClrType, <span style="color:#0000ff;">out</span> bulkDeletedFuncs))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum321"> 321:</span>                         bulkDeletedFuncs.Add(npPredicate);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum322"> 322:</span>                     <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum323"> 323:</span>                         BulkDeletedFuncs.Add(subEntityType.ClrType, <span style="color:#0000ff;">new</span> List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt;() { npPredicate });</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum324"> 324:</span>                 }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum325"> 325:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum326"> 326:</span>             <span style="color:#0000ff;">foreach</span> (var entity <span style="color:#0000ff;">in</span> ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged).</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum327"> 327:</span>                                     Select(ose =&gt; <span style="color:#0000ff;">new</span> { Entity = ose.Entity <span style="color:#0000ff;">as</span> T, ose.State }).</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum328"> 328:</span>                                     Where(e =&gt; e.Entity != <span style="color:#0000ff;">null</span> &amp;&amp; predicateCompiled(e.Entity)))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum329"> 329:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum330"> 330:</span>                 <span style="color:#0000ff;">if</span> (entity.State != EntityState.Deleted)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum331"> 331:</span>                     DeleteObjectAndAddThemIntoBulkDeletedEntities(entity.Entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum332"> 332:</span>                 <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum333"> 333:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum334"> 334:</span>                     BulkDeletedEntities.Add(entity.Entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum335"> 335:</span>                     <span style="color:#0000ff;">foreach</span> (var subEntity <span style="color:#0000ff;">in</span> oneToOneSubEntityTypes.</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum336"> 336:</span>                                                 SelectMany(subEntityType =&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum337"> 337:</span>                                                     ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged).</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum338"> 338:</span>                                                     Where(ose =&gt; subEntityType.ClrType.IsAssignableFrom(ose.Entity.GetType()) &amp;&amp; !BulkDeletedEntities.Contains(ose.Entity))))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum339"> 339:</span>                         ApplyBulkDeletedFuncs(subEntity.Entity, subEntity.State);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum340"> 340:</span>                 }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum341"> 341:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum342"> 342:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum343"> 343:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum344"> 344:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> ApplyBulkDeletedFuncs(<span style="color:#0000ff;">object</span> entity, EntityState entityState)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum345"> 345:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum346"> 346:</span>             List&lt;Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt;&gt; bulkDeletedFuncs;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum347"> 347:</span>             <span style="color:#0000ff;">if</span> (_bulkDeletedFuncs != <span style="color:#0000ff;">null</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum348"> 348:</span>             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum349"> 349:</span>                 Type t = entity.GetType();</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum350"> 350:</span>                 <span style="color:#0000ff;">do</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum351"> 351:</span>                 {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum352"> 352:</span>                     <span style="color:#0000ff;">if</span> (BulkDeletedFuncs.TryGetValue(t, <span style="color:#0000ff;">out</span> bulkDeletedFuncs))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum353"> 353:</span>                         <span style="color:#0000ff;">foreach</span> (Func&lt;<span style="color:#0000ff;">object</span>, <span style="color:#0000ff;">bool</span>&gt; bulkDeletedFunc <span style="color:#0000ff;">in</span> bulkDeletedFuncs)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum354"> 354:</span>                             <span style="color:#0000ff;">if</span> (bulkDeletedFunc(entity))</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum355"> 355:</span>                             {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum356"> 356:</span>                                 <span style="color:#0000ff;">if</span> (entityState != EntityState.Deleted)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum357"> 357:</span>                                     DeleteObjectAndAddThemIntoBulkDeletedEntities(entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum358"> 358:</span>                                 <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum359"> 359:</span>                                     BulkDeletedEntities.Add(entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum360"> 360:</span>                                 <span style="color:#0000ff;">return</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum361"> 361:</span>                             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum362"> 362:</span>                 } <span style="color:#0000ff;">while</span> ((t = t.BaseType) != <span style="color:#0000ff;">null</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum363"> 363:</span>             }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum364"> 364:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum365"> 365:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum366"> 366:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> DeleteObjectAndAddThemIntoBulkDeletedEntities(<span style="color:#0000ff;">object</span> entity)</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum367"> 367:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum368"> 368:</span>             CollectionChangeEventHandler objectStateManagerObjectStateManagerChanged = (sender, e) =&gt; BulkDeletedEntities.Add(e.Element);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum369"> 369:</span>             ObjectStateManager.ObjectStateManagerChanged += objectStateManagerObjectStateManagerChanged;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum370"> 370:</span>             DeleteObject(entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum371"> 371:</span>             ObjectStateManager.ObjectStateManagerChanged -= objectStateManagerObjectStateManagerChanged;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum372"> 372:</span>             BulkDeletedEntities.Add(entity);</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum373"> 373:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum374"> 374:</span>         <span style="color:#cc6633;">#endregion</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum375"> 375:</span>  </pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum376"> 376:</span>     }</pre>
<p><!--CRLF--></div>
</div>
<p style="background:white;margin:0 0 10pt;" class="MsoNormal"><span style="font-family:'Courier New';color:black;" lang="EN"><font size="2">you can add or remove these features according your needs. Here i have tried to provide you some of written code which I think used in more or less in every application. Good luck.</font></span></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmorshedanwar.wordpress.com%2f2011%2f06%2f30%2fimplementing-repository-pattern-with-ef4-poco-support%2f"><img border="0" alt="kick it on DotNetKicks.com" src="http%3A%2F%2Fwww.dotnetkicks.com%2FServices%2FImages%2FKickItImageGenerator.ashx%3Furl%3Dhttp%253a%252f%252fmorshedanwar.wordpress.com%252f2011%252f06%252f30%252fimplementing-repository-pattern-with-ef4-poco-support%252f"/></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=154&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2011/06/30/implementing-repository-pattern-with-ef4-poco-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>

		<media:content url="http://http%3A%2F%2Fwww.dotnetkicks.com%2FServices%2FImages%2FKickItImageGenerator.ashx%3Furl%3Dhttp%253a%252f%252fmorshedanwar.wordpress.com%252f2011%252f06%252f30%252fimplementing-repository-pattern-with-ef4-poco-support%252f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Ado.net Entity Framework Tutorial</title>
		<link>http://morshedanwar.wordpress.com/2009/07/14/ado-net-entity-framework-tutorial/</link>
		<comments>http://morshedanwar.wordpress.com/2009/07/14/ado-net-entity-framework-tutorial/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 12:27:28 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/07/14/ado-net-entity-framework-tutorial/</guid>
		<description><![CDATA[Joydip Kanjilal&#160;has released a book on ADO.NET Entity Framework entitled&#160;Entity Framework Tutorial .Its a very good thing to jump over the Code on Entity framework and make clear the mechanism concept of EF. I am very thankful to the publication of this book to provide me an opportunity to review this book specially the time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=134&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span class="Apple-style-span" style="word-spacing:0;font:16px &#39;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;"><span class="Apple-style-span" style="font-size:13px;color:rgb(85,85,85);line-height:20px;font-family:verdana;"><a style="color:rgb(77,102,128);border-bottom:rgb(144,178,213) 1px dotted;text-decoration:none;margin:0;padding:0;" href="http://aspadvice.com/blogs/joydip/">Joydip Kanjilal</a><span class="Apple-converted-space">&#160;</span>has released a book on ADO.NET Entity Framework entitled<span class="Apple-converted-space">&#160;</span><em>Entity Framework Tutorial .</em>Its a very good thing to jump over the Code on Entity framework and make clear the mechanism concept of EF. I am very thankful to the publication of this book to provide me an opportunity to review this book specially the time when I didn&#8217;t find much resource on the framework. It gave me some good idea about E<span class="Apple-style-span" style="word-spacing:0;font:16px &#39;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;"><span class="Apple-style-span" style="font-size:13px;color:rgb(85,85,85);line-height:20px;font-family:verdana;">ntity Data Model (EDM), using E-SQL and LINQ to Entities to perform queries, and even an introduction to ADO.NET Data Services (Astoria). <span class="Apple-style-span" style="word-spacing:0;font:16px &#39;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;"><span class="Apple-style-span" style="font-size:13px;color:rgb(85,85,85);line-height:20px;font-family:verdana;"><em>Entity Framework Tutorial</em><span class="Apple-converted-space">&#160;</span>located<span class="Apple-converted-space">&#160;</span><a style="color:rgb(77,102,128);border-bottom:rgb(144,178,213) 1px dotted;text-decoration:none;margin:0;padding:0;" href="http://www.dotnetjohn.com/articles.aspx?articleid=274">here</a>. Finally, scripts and code downloads for this book are available<span class="Apple-converted-space">&#160;</span><a style="color:rgb(77,102,128);border-bottom:rgb(144,178,213) 1px dotted;text-decoration:none;margin:0;padding:0;" href="http://www.packtpub.com/support/book/entity-framework-tutorial">here</a>. you can also go to it’s publication site to get more information about this “Good Start” book <a href="http://www.packtpub.com/entity-framework-tutorial/book">Here</a>.</span></span></span></span></span></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=134&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/07/14/ado-net-entity-framework-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing Repository Pattern With Entity Framework</title>
		<link>http://morshedanwar.wordpress.com/2009/06/10/implementing-repository-pattern-with-entity-framework/</link>
		<comments>http://morshedanwar.wordpress.com/2009/06/10/implementing-repository-pattern-with-entity-framework/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 05:58:00 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/06/09/implementing-repository-pattern-with-entity-framework/</guid>
		<description><![CDATA[Click here to Get the Sample code of Repository Implementation My Recent published article in CodeProject is also available here in my blog post for those my friends who feels a little interest(!!) to read my blog post. The Repository pattern is defined by P of EAA as: Mediates between the domain and data mapping [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=128&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;"><a href="http://cid-198dc28bb8d4b104.skydrive.live.com/self.aspx/.Public/SampleCode/EFRepositoryExample.zip" target="_blank">Click here to Get the Sample code of Repository Implementation</a></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;"><a href="http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx">My Recent published article in CodeProject</a> is also available here in my blog post for those my friends who feels a little interest(!!) to read my blog post. </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;">The Repository pattern is defined by P of EAA as: </span></p>
</p>
</p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><font size="3"><strong><i><span style="font-family:consolas;">Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.</span></i></strong><strong><i><span style="font-size:10pt;background:yellow;font-family:consolas;"> </span></i></strong></font></p>
</p>
</p>
</p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;">Repository provides a in-memory like collection interface for accessing domain objects. So as far as the consuming component is concerned, it uses the repository just like a collection when working with Domain objects. The repository then neatly abstracts the internal mechanics of how the Add / Remove calls to the repository translate to the actual data access calls to the data store. </span><strong><span style="font-weight:normal;font-size:10pt;font-family:consolas;">Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.</span></strong><b><i><span style="font-family:consolas;"> </span></i></b></p>
</p>
</p>
</p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;">So with the repository we get a nice abstraction that provides us with persistence ignorance and a nice separation of concerns where the responsibility of persisting domain objects is encapsulated by the Repository that <span>&#160;</span>leaving the domain objects to deal entirely with the domain model and domain logic. </span></p>
</p>
</p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><span style="font-size:9pt;font-family:consolas;">Here I use the Composite Repository pattern.</span><strong><i><span style="font-family:consolas;"> </span></i></strong></p>
<p> <a href="http://11011.net/software/vspaste"></a>
</p>
</p>
</p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Repository Interface defines the base
</span><span style="color:gray;">/// </span><span style="color:green;">functionality required by all Repositories.
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;typeparam name=&quot;T&quot;&gt;
/// </span><span style="color:green;">The entity type that requires a Repository.
</span><span style="color:gray;">/// &lt;/typeparam&gt;
</span><span style="color:blue;">public interface </span><span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;
{
    <span style="color:#2b91af;">DbTransaction </span>BeginTransaction();
    <span style="color:blue;">void </span>Add(E entity);
    <span style="color:blue;">void </span>AddOrAttach(E entity);
    <span style="color:blue;">void </span>DeleteRelatedEntries(E entity);
    <span style="color:blue;">void </span>DeleteRelatedEntries(E entity, <span style="color:#2b91af;">ObservableCollection</span>&lt;<span style="color:blue;">string</span>&gt; keyListOfIgnoreEntites);
    <span style="color:blue;">void </span>Delete(E entity);
    <span style="color:blue;">int </span>Save();

    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName);
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery();
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where);
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where);
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex);
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression);
    <span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex);

    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName);
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll();
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where);
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where);
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex);
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression);
    <span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex);

    E SelectByKey(<span style="color:blue;">string </span>Key);

    <span style="color:blue;">bool </span>TrySameValueExist(<span style="color:blue;">string </span>fieldName, <span style="color:blue;">object </span>fieldValue, <span style="color:blue;">string </span>key);
    <span style="color:blue;">bool </span>TryEntity(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec);

    <span style="color:blue;">int </span>GetCount();
    <span style="color:blue;">int </span>GetCount(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;">You can write your own Repository for each business object like RoleReporitosy, UserReporsitory etc etc&#8230;or You can Implement this interface as a generic somthing like this -</span></p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Repository</span>&lt;E, C&gt; : <span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;, <span style="color:#2b91af;">IDisposable
    </span><span style="color:blue;">where </span>E : <span style="color:#2b91af;">EntityObject
    </span><span style="color:blue;">where </span>C : <span style="color:#2b91af;">ObjectContext
</span>{
    <span style="color:blue;">private readonly </span>C _ctx;

    <span style="color:blue;">private string </span>_KeyProperty = <span style="color:#a31515;">&quot;ID&quot;</span>;

    <span style="color:blue;">public string </span>KeyProperty
    {
        <span style="color:blue;">get
        </span>{
            <span style="color:blue;">return </span>_KeyProperty;
        }
        <span style="color:blue;">set
        </span>{
            _KeyProperty = <span style="color:blue;">value</span>;
        }
    }

    <span style="color:blue;">public </span>C Session
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>_ctx; }
    }

    <span style="color:blue;">public </span>Repository(C session)
    {
        _ctx = session;
    }

    <span style="color:blue;">#region </span>IRepository&lt;E,C&gt; Members

    <span style="color:blue;">public int </span>Save()
    {
        <span style="color:blue;">return </span>_ctx.SaveChanges();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. In Version
    </span><span style="color:gray;">/// </span><span style="color:green;">2.0 this pluralizing behaviour will change and this
    </span><span style="color:gray;">/// </span><span style="color:green;">method overload should be used only if Developers
    </span><span style="color:gray;">/// </span><span style="color:green;">change EntitySet names from the default name generated.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName)
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ entitySetName + <span style="color:#a31515;">&quot;]&quot;</span>);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery()
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return
            </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ entitySetName + <span style="color:#a31515;">&quot;]&quot;</span>)
            .Where(where.EvalPredicate);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return
            </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>)
            .Where(where.EvalPredicate);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query Entity with Paging
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;maximumRows&quot;&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;startRowIndex&quot;&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>).Skip&lt;E&gt;(startRowIndex).Take(maximumRows);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query Entity in sorted Order
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;sortExpression&quot;&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;ErrorCode&quot;&gt;</span><span style="color:green;">custom Error Message</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression)
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>== sortExpression)
        {
            <span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery();
        }
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery().OrderBy&lt;E, T&gt;(sortExpression);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Query All Entity in sorted Order with Paging support
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;sortExpression&quot;&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;maximumRows&quot;&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;startRowIndex&quot;&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection Of entites</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt; DoQuery&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">if </span>(sortExpression == <span style="color:blue;">null</span>)
        {
            <span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery(maximumRows, startRowIndex);
        }
        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ObjectQuery</span>&lt;E&gt;)((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).DoQuery(sortExpression).Skip&lt;E&gt;(startRowIndex).Take(maximumRows);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. In Version
    </span><span style="color:gray;">/// </span><span style="color:green;">2.0 this pluralizing behaviour will change and this
    </span><span style="color:gray;">/// </span><span style="color:green;">method overload should be used only if Developers
    </span><span style="color:gray;">/// </span><span style="color:green;">change EntitySet names from the default name generated.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName)
    {
        <span style="color:blue;">return </span>DoQuery(entitySetName).ToList();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll()
    {
        <span style="color:blue;">try
        </span>{
            <span style="color:blue;">return </span>DoQuery().ToList(); <span style="color:green;">//_ctx.CreateQuery&lt;E&gt;(&quot;[&quot; + typeof(E).Name + &quot;]&quot;);
        </span>}
        <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception</span>)
        {
            <span style="color:blue;">throw</span>;
        }
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">the parameter entitySetName in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=”entitySetName”&gt;
    /// </span><span style="color:green;">The EntitySet name of the entity in the model.
    </span><span style="color:gray;">/// &lt;/param&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">string </span>entitySetName, <span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return </span>DoQuery(entitySetName, where).ToList();
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A generic method to return ALL the entities
    </span><span style="color:gray;">/// </span><span style="color:green;">of type TEntity. This overload will use the
    </span><span style="color:gray;">/// </span><span style="color:green;">typeof(TEntity).Name in the resolution of mapping
    </span><span style="color:gray;">/// </span><span style="color:green;">between the CSDL and SSDL. This method is useful
    </span><span style="color:gray;">/// </span><span style="color:green;">for Models that DO NOT have their pluralized names
    </span><span style="color:gray;">/// </span><span style="color:green;">changed by the developer.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=”TEntity”&gt;
    /// </span><span style="color:green;">The Entity to load from the database.
    </span><span style="color:gray;">/// &lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Returns a set of TEntity.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; where)
    {
        <span style="color:blue;">return </span>DoQuery(where).ToList();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity with Paging
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;maximumRows&quot;&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;startRowIndex&quot;&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll(<span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">return </span>DoQuery(maximumRows, startRowIndex).ToList();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity in sorted Order
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;sortExpression&quot;&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;ErrorCode&quot;&gt;</span><span style="color:green;">custom Error Message</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection of Entities</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression)
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>== sortExpression)
        {
            <span style="color:blue;">return </span>DoQuery(sortExpression).ToList();
        }
        <span style="color:blue;">return </span>DoQuery(sortExpression).ToList();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select All Entity in sorted Order with Paging support
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;sortExpression&quot;&gt;</span><span style="color:green;">Sort Expression/condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;maximumRows&quot;&gt;</span><span style="color:green;">Max no of row to Fetch</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;startRowIndex&quot;&gt;</span><span style="color:green;">Start Index</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">Collection Of entites</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">IList</span>&lt;E&gt; SelectAll&lt;T&gt;(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, T&gt;&gt; sortExpression, <span style="color:blue;">int </span>maximumRows, <span style="color:blue;">int </span>startRowIndex)
    {
        <span style="color:blue;">if </span>(sortExpression == <span style="color:blue;">null</span>)
        {
            <span style="color:blue;">return </span>DoQuery(maximumRows, startRowIndex).ToList();
        }
        <span style="color:blue;">return </span>DoQuery(sortExpression, maximumRows, startRowIndex).ToList();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get Entity By Primary Key
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;</span><span style="color:green;">Entity Type</span><span style="color:gray;">&lt;/typeparam&gt;
    /// &lt;param name=&quot;Key&quot;&gt;</span><span style="color:green;">Primary Key Value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">return entity</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public </span>E SelectByKey(<span style="color:blue;">string </span>Key)
    {
        <span style="color:green;">// First we define the parameter that we are going to use the clause.
        </span><span style="color:blue;">var </span>xParam = <span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(E), <span style="color:blue;">typeof</span>(E).Name);
        <span style="color:#2b91af;">MemberExpression </span>leftExpr = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, <span style="color:blue;">this</span>._KeyProperty);
        <span style="color:#2b91af;">Expression </span>rightExpr = <span style="color:#2b91af;">Expression</span>.Constant(Key);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExpr = <span style="color:#2b91af;">MemberExpression</span>.Equal(leftExpr, rightExpr);
        <span style="color:green;">//Create Lambda Expression for the selection
        </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; lambdaExpr = <span style="color:#2b91af;">Expression</span>.Lambda&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt;(binaryExpr, <span style="color:blue;">new </span><span style="color:#2b91af;">ParameterExpression</span>[] { xParam });
        <span style="color:green;">//Searching ....
        </span><span style="color:#2b91af;">IList</span>&lt;E&gt; resultCollection = ((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).SelectAll(<span style="color:blue;">new </span><span style="color:#2b91af;">Specification</span>&lt;E&gt;(lambdaExpr));
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= resultCollection &amp;&amp; resultCollection.Count() &gt; 0)
        {
            <span style="color:green;">//return valid single result
            </span><span style="color:blue;">return </span>resultCollection.First&lt;E&gt;();
        }<span style="color:green;">//end if
        </span><span style="color:blue;">return null</span>;
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Check if value of specific field is already exist
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;&lt;/typeparam&gt;
    /// &lt;param name=&quot;fieldName&quot;&gt;</span><span style="color:green;">name of the Field</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;fieldValue&quot;&gt;</span><span style="color:green;">Field value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;key&quot;&gt;</span><span style="color:green;">Primary key value</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">True or False</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public bool </span>TrySameValueExist(<span style="color:blue;">string </span>fieldName, <span style="color:blue;">object </span>fieldValue, <span style="color:blue;">string </span>key)
    {
        <span style="color:green;">// First we define the parameter that we are going to use the clause.
        </span><span style="color:blue;">var </span>xParam = <span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(E), <span style="color:blue;">typeof</span>(E).Name);
        <span style="color:#2b91af;">MemberExpression </span>leftExprFieldCheck = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, fieldName);
        <span style="color:#2b91af;">Expression </span>rightExprFieldCheck = <span style="color:#2b91af;">Expression</span>.Constant(fieldValue);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExprFieldCheck = <span style="color:#2b91af;">MemberExpression</span>.Equal(leftExprFieldCheck, rightExprFieldCheck);

        <span style="color:#2b91af;">MemberExpression </span>leftExprKeyCheck = <span style="color:#2b91af;">MemberExpression</span>.Property(xParam, <span style="color:blue;">this</span>._KeyProperty);
        <span style="color:#2b91af;">Expression </span>rightExprKeyCheck = <span style="color:#2b91af;">Expression</span>.Constant(key);
        <span style="color:#2b91af;">BinaryExpression </span>binaryExprKeyCheck = <span style="color:#2b91af;">MemberExpression</span>.NotEqual(leftExprKeyCheck, rightExprKeyCheck);
        <span style="color:#2b91af;">BinaryExpression </span>finalBinaryExpr = <span style="color:#2b91af;">Expression</span>.And(binaryExprFieldCheck, binaryExprKeyCheck);

        <span style="color:green;">//Create Lambda Expression for the selection
        </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; lambdaExpr = <span style="color:#2b91af;">Expression</span>.Lambda&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt;(finalBinaryExpr, <span style="color:blue;">new </span><span style="color:#2b91af;">ParameterExpression</span>[] { xParam });
        <span style="color:green;">//Searching ....
        </span><span style="color:blue;">return </span>((<span style="color:#2b91af;">IRepository</span>&lt;E, C&gt;)<span style="color:blue;">this</span>).TryEntity(<span style="color:blue;">new </span><span style="color:#2b91af;">Specification</span>&lt;E&gt;(lambdaExpr));
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Check if Entities exist with Condition
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;selectExpression&quot;&gt;</span><span style="color:green;">Selection Condition</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">True or False</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public bool </span>TryEntity(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec)
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>).Any&lt;E&gt;(selectSpec.EvalPredicate);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get Count of all records
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;&lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">count of all records</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public int </span>GetCount()
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>).Count();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get count of selection
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;</span><span style="color:green;">Selection Condition</span><span style="color:gray;">&lt;/typeparam&gt;
    /// &lt;returns&gt;</span><span style="color:green;">count of selection</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public int </span>GetCount(<span style="color:#2b91af;">ISpecification</span>&lt;E&gt; selectSpec)
    {
        <span style="color:blue;">return </span>_ctx.CreateQuery&lt;E&gt;(<span style="color:#a31515;">&quot;[&quot; </span>+ <span style="color:blue;">typeof</span>(E).Name + <span style="color:#a31515;">&quot;]&quot;</span>)
            .Where(selectSpec.EvalPredicate).Count();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Delete data from context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;&lt;/typeparam&gt;
    /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>Delete(E entity)
    {
        _ctx.DeleteObject(entity);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Insert new data into context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;E&quot;&gt;&lt;/typeparam&gt;
    /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>Add(E entity)
    {
        _ctx.AddObject(entity.GetType().Name, entity);
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Insert if new otherwise attach data into context
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>AddOrAttach(E entity)
    {
        <span style="color:green;">// Define an ObjectStateEntry and EntityKey for the current object.
        </span><span style="color:#2b91af;">EntityKey </span>key;
        <span style="color:blue;">object </span>originalItem;
        <span style="color:green;">// Get the detached object's entity key.
        </span><span style="color:blue;">if </span>(entity.EntityKey == <span style="color:blue;">null</span>)
        {
            <span style="color:green;">// Get the entity key of the updated object.
            </span>key = _ctx.CreateEntityKey(entity.GetType().Name, entity);
        }
        <span style="color:blue;">else
        </span>{
            key = entity.EntityKey;
        }
        <span style="color:blue;">try
        </span>{
            <span style="color:green;">// Get the original item based on the entity key from the context
            // or from the database.
            </span><span style="color:blue;">if </span>(_ctx.TryGetObjectByKey(key, <span style="color:blue;">out </span>originalItem))
            {<span style="color:green;">//accept the changed property
                </span><span style="color:blue;">if </span>(originalItem <span style="color:blue;">is </span><span style="color:#2b91af;">EntityObject </span>&amp;&amp;
                    ((<span style="color:#2b91af;">EntityObject</span>)originalItem).EntityState != <span style="color:#2b91af;">EntityState</span>.Added)
                {
                    <span style="color:green;">// Call the ApplyPropertyChanges method to apply changes
                    // from the updated item to the original version.
                    </span>_ctx.ApplyPropertyChanges(
                        key.EntitySetName, entity);
                }
            }
            <span style="color:blue;">else
            </span>{<span style="color:green;">//add the new entity
                </span>Add(entity);
            }<span style="color:green;">//end else
        </span>}
        <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception </span>ex)
        {
            <span style="color:blue;">throw </span>ex;
        }
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Start Transaction
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">DbTransaction </span>BeginTransaction()
    {
        <span style="color:blue;">if </span>(_ctx.Connection.State != <span style="color:#2b91af;">ConnectionState</span>.Open)
        {
            _ctx.Connection.Open();
        }
        <span style="color:blue;">return </span>_ctx.Connection.BeginTransaction();
    }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Delete all realted entries
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>DeleteRelatedEntries(E entity)
    {
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>relatedEntity <span style="color:blue;">in </span>(((<span style="color:#2b91af;">IEntityWithRelationships</span>)entity).RelationshipManager.GetAllRelatedEnds().SelectMany(re =&gt; re.CreateSourceQuery().OfType&lt;<span style="color:#2b91af;">EntityObject</span>&gt;()).Distinct()).ToArray())
        {
            _ctx.DeleteObject(relatedEntity);
        }<span style="color:green;">//end foreach
    </span>}
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Delete all realted entries
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">public void </span>DeleteRelatedEntries(E entity, <span style="color:#2b91af;">ObservableCollection</span>&lt;<span style="color:blue;">string</span>&gt; keyListOfIgnoreEntites)
    {
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>relatedEntity <span style="color:blue;">in </span>(((<span style="color:#2b91af;">IEntityWithRelationships</span>)entity).RelationshipManager.GetAllRelatedEnds().SelectMany(re =&gt; re.CreateSourceQuery().OfType&lt;<span style="color:#2b91af;">EntityObject</span>&gt;()).Distinct()).ToArray())
        {
            <span style="color:#2b91af;">PropertyInfo </span>propInfo = relatedEntity.GetType().GetProperty(<span style="color:blue;">this</span>._KeyProperty);
            <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= propInfo)
            {
                <span style="color:blue;">string </span>value = (<span style="color:blue;">string</span>)propInfo.GetValue(relatedEntity, <span style="color:blue;">null</span>);
                <span style="color:blue;">if </span>(!<span style="color:blue;">string</span>.IsNullOrEmpty(value) &amp;&amp;
                    keyListOfIgnoreEntites.Contains(value))
                {
                    <span style="color:blue;">continue</span>;
                }<span style="color:green;">//end if
            </span>}<span style="color:green;">//end if
            </span>_ctx.DeleteObject(relatedEntity);
        }<span style="color:green;">//end foreach
    </span>}

    <span style="color:blue;">#endregion

    #region </span>IDisposable Members

    <span style="color:blue;">public void </span>Dispose()
    {
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= _ctx)
        {
            _ctx.Dispose();
        }
    }

    <span style="color:blue;">#endregion

</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span class="Apple-style-span" style="word-spacing:0;font:13px verdana;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;"><span class="Apple-style-span" style="font-size:12px;line-height:13px;font-family:consolas;">Notice that , Here I also implement IDicpose interface to dispose the context manually.To get the name of Entityset, Here I have used<span class="Apple-converted-space">&#160;</span><code style="font-size:11pt;color:rgb(153,0,0);font-family:&#39;">typeof</code></span>&#160; ,<span class="Apple-style-span" style="font-size:12px;line-height:13px;font-family:consolas;">but you can you metadata query to retrieve the entityset name.</span></span></span></p>
<pre class="code"><span style="color:blue;">var </span>container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
<span style="color:blue;">string </span>entitySetName = (<span style="color:blue;">from </span>meta <span style="color:blue;">in </span>container.BaseEntitySets
                        <span style="color:blue;">where </span>meta.ElementType.Name == entityTypeName
                        <span style="color:blue;">select </span>meta.Name).FirstOrDefault();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span class="Apple-style-span" style="word-spacing:0;font:12px/13px consolas;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;"><span class="Apple-style-span" style="word-spacing:0;font:12px/13px consolas;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;">Here I am not gointo the code.&#160; </span></span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span class="Apple-style-span" style="word-spacing:0;font:12px/13px consolas;text-transform:none;color:rgb(0,0,0);text-indent:0;white-space:normal;letter-spacing:normal;border-collapse:separate;orphans:2;widows:2;">The specification pattern we can implement a re-usable business logic component that can be passed around to satisfy certain business criteria . <span class="Apple-style-span" style="font-size:13px;line-height:normal;font-family:consolas;">The specification object has a clear and limited responsibility, which can be separated and decoupled from the domain object that uses it.<span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:12px;line-height:13px;font-family:consolas;">&#160;<span class="Apple-style-span" style="font-size:13px;line-height:normal;font-family:verdana;"><span style="font-size:9pt;line-height:115%;font-family:consolas;">I would highly recommend reading the white paper by<span class="apple-converted-space">&#160;</span></span><span style="font-size:11pt;line-height:115%;font-family:consolas;"><a style="text-decoration:none;" href="http://martinfowler.com/apsupp/spec.pdf" target="_blank"><span style="font-size:9pt;line-height:115%;"><font color="#800080">Martin Fowler and Eric Evans on the Specification pattern</font></span></a></span><span style="font-size:9pt;line-height:115%;font-family:consolas;">.<span class="Apple-style-span" style="font-size:13px;line-height:normal;font-family:verdana;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span class="Apple-style-span" style="font-family:consolas;"><span class="Apple-style-span" style="font-size:13px;line-height:normal;font-family:verdana;"><span style="font-size:9pt;line-height:115%;font-family:consolas;">&#160;</span></span></span></span></span></span></span></span></span></span></span></span></p>
</p>
<pre class="code"><span style="color:blue;">public interface </span><span style="color:#2b91af;">ISpecification</span>&lt;E&gt;
{
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Select/Where Expression
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; EvalPredicate { <span style="color:blue;">get</span>; }
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Function to evaluate where Expresiion
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt; EvalFunc { <span style="color:blue;">get</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="font-size:9pt;line-height:115%;font-family:consolas;">You can write your own specification by implementing your interface like RoleSpecification and put down your business logic there.For general use you can also Implement this composite specification interface like this- </span></p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Specification</span>&lt;E&gt; : <span style="color:#2b91af;">ISpecification</span>&lt;E&gt;
{
    <span style="color:blue;">#region </span>Private Members

    <span style="color:blue;">private </span><span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt; _evalFunc = <span style="color:blue;">null</span>;
    <span style="color:blue;">private </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; _evalPredicate;

    <span style="color:blue;">#endregion

    #region </span>Virtual Accessors

    <span style="color:blue;">public virtual </span><span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; EvalPredicate
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>_evalPredicate; }
    }

    <span style="color:blue;">public virtual </span><span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt; EvalFunc
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>_evalFunc; }
    }

    <span style="color:blue;">#endregion

    #region </span>Constructors

    <span style="color:blue;">public </span>Specification(<span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;E, <span style="color:blue;">bool</span>&gt;&gt; predicate)
    {
        _evalPredicate = predicate;
    }

    <span style="color:blue;">private </span>Specification() { }

    <span style="color:blue;">#endregion
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">To use this </span><span style="font-size:10pt;line-height:115%;font-family:&quot;">Repository</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> and </span><span style="font-size:10pt;line-height:115%;font-family:&quot;">Specification</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> class from your business layer, one thing I must have to say that I am sharing a single context in the CRUD process/operation. You must keep alive the context in your entire process of a crud operation. For this I make a Global variable of my EF </span><span style="font-size:10pt;line-height:115%;font-family:&quot;">ObjectContext</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> and <span>&#160;</span><span>&#160;</span>all using repository and in the constructor I initialize all this variables like this- </span></p>
</p>
</p>
<pre class="code"><span style="color:blue;">#region </span>Global members

<span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">User</span>, SecurityEntities&gt; _userRepository = <span style="color:blue;">null</span>;
<span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">Role</span>, SecurityEntities&gt; _roleRepository = <span style="color:blue;">null</span>;
<span style="color:#2b91af;">Repository</span>&lt; Task, JerichoSecurityEntities&gt; _taskRepository = <span style="color:blue;">null</span>;
<span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">RoleInUser</span>,SecurityEntities&gt; _roleInUserRepository
         = <span style="color:blue;">null</span>;
<span style="color:#2b91af;">Repository</span>&lt; RoleTask,SecurityEntities&gt; _roleTaskRepository
         = <span style="color:blue;">null</span>;
<span style="color:#2b91af;">Repository</span>&lt; DBAuditForSecurity, SecurityEntities&gt; _securityAuditRepository

SecurityEntities _ctx = <span style="color:blue;">null</span>;
<span style="color:blue;">#endregion

public </span>BLLRoleManagement()
{
    _ctx = Helper.SecurityContextInstance;
    _userRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">User</span>,SecurityEntities&gt;(_ctx);
    _roleRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">Role</span>, SecurityEntities&gt;(_ctx);
    _taskRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; Task, SecurityEntities&gt;(_ctx);
    _roleInUserRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; <span style="color:#2b91af;">RoleInUser</span>, SecurityEntities&gt;(_ctx);
    _roleTaskRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; RoleTask, SecurityEntities&gt;(_ctx);
    _securityAuditRepository = <span style="color:blue;">new </span><span style="color:#2b91af;">Repository</span>&lt; DBAuditForSecurity, SecurityEntities&gt;(_ctx);
}</pre>
<pre class="code">………………………….</pre>
<pre class="code">……………………………………………</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">Now In my </span><span style="font-size:10pt;line-height:115%;font-family:&quot;">BLLRoleManagement</span><span style="font-size:10pt;line-height:115%;font-family:consolas;">, say I have method to delete User. That will be something like this – </span></p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Delete User
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;param name=&quot;userID&quot;&gt;</span><span style="color:green;">User ID</span><span style="color:gray;">&lt;/param&gt;
/// &lt;returns&gt;</span><span style="color:green;">True Or False</span><span style="color:gray;">&lt;/returns&gt;
</span><span style="color:blue;">public static bool </span>DeleteUser(
    <span style="color:blue;">string </span>UserID)
{
    <span style="color:blue;">try
    </span>{
        <span style="color:blue;">using </span>(DbTransaction transaction = userRepository.BeginTransaction())
        {
            <span style="color:#2b91af;">User </span>UserTobeRemoved = userRepository.SelectByKey(UserID);
            <span style="color:blue;">if </span>(UserTobeRemoved == <span style="color:blue;">null</span>)
                <span style="color:blue;">return false</span>;

            userRepository.DeleteRelatedEntries(UserTobeRemoved);
            userRepository.Delete(UserTobeRemoved);
            <span style="color:blue;">if </span>(userRepository.Save() &gt;= 0)
            {
                transaction.Commit();
                <span style="color:blue;">return true</span>;
            }
        }
    }
    <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception </span>ex)
    {
        <span style="color:blue;">throw </span>ErrorHandler.WrapException(ex, ErrorCodes.DB_DeleteUser_Error_Code);
    }
    <span style="color:blue;">return false</span>;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">So You can call this method like &#8211; </span></p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;max-height:200px;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:white;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">new</span> BLLRoleManagement().DeleteUser(userID);</pre>
<p><!--CRLF--></div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=128&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/06/10/implementing-repository-pattern-with-entity-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework: Sorting EntitySets According to parent-child relationship using Context properties</title>
		<link>http://morshedanwar.wordpress.com/2009/06/05/sorted-entityset-according-to-parent-child-relationship/</link>
		<comments>http://morshedanwar.wordpress.com/2009/06/05/sorted-entityset-according-to-parent-child-relationship/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 08:31:00 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/06/05/sorted-entity-set-according-to-parent-child-relationship/</guid>
		<description><![CDATA[Some month ago while I was working with Entity Framework V1 in data access layer, Client ask me to make a backup of the db&#160; from a generic place. Because client wants a flexibility to use any db storage(MS Sql server ,MySql, may be VistaDB ) with same schema. So I need to make a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=108&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">Some month ago while I was working with Entity Framework V1 in data access layer, Client ask me to make a backup of the db<span>&#160; </span>from a generic place. Because client wants a flexibility to use any db storage(MS Sql server ,MySql, may be VistaDB ) with same schema. So I need to make a backup system from a generic place. So what I did (I don’t know it was a good idea or not) was that, I serialized the all the </span><span style="font-size:10pt;line-height:115%;font-family:consolas;">EntitySet</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> graphs and store into some files and then compressed and encrypt them in a single file. <span>&#160;</span>So, when a user wants to restore the DB , I need to <span>&#160;</span>decrypt and de-serialized all the entity objects and attach or add the DB . To </span><span style="font-size:10pt;line-height:115%;font-family:consolas;">attach or Add</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> I have to maintain the order of parent child relationship. So When I store in into File(what I say as Backup) I follow the parent child relationship of the Entity set and maintain the order like – Root First and then The child. During restore I also have to do the reverse way. I mean to say I have </span><span style="font-size:10pt;line-height:115%;font-family:consolas;">attch or add</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> the child first and then the parent. So to do that I have sort the entity set in a parent Child relationship. To make this, first I make a dictionary where I find the references of each entity set. So the key was the Type entity set and value is List of Type of Entity Set those are references the the key Entity set.</span></p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Get Current Relationship Dictionary of Entites
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;returns&gt;</span><span style="color:green;">Dictionary of Current Entites</span><span style="color:gray;">&lt;/returns&gt;
</span><span style="color:blue;">public static </span><span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt; GetCurrentDBEntityRelationshipChildren&lt;T&gt;() <span style="color:blue;">where </span>T : ObjectContex
{
    <span style="color:blue;">try
    </span>{
        <span style="color:green;">//Take a dictionary
        </span><span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt; objectCollection = <span style="color:blue;">new </span><span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt;();
        <span style="color:green;">//Get al properties contex
        </span>PropertyInfo[] properties = <span style="color:blue;">typeof</span>(T).GetProperties();

        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>item <span style="color:blue;">in </span>properties)
        {<span style="color:green;">//check
            //Pickup the properties only those represent entitys of database
            </span><span style="color:blue;">if </span>(item.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(ObjectQuery)))
            {
                <span style="color:green;">//Get Propety value
                </span><span style="color:#2b91af;">Type </span>entityType = item.PropertyType.GetGenericArguments().First(); ;
                <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= entityType)
                {
                    <span style="color:blue;">try
                    </span>{
                        AddToDictionary(objectCollection, entityType, <span style="color:blue;">null</span>);
                        PropertyInfo[] entityProperties = entityType.GetProperties();

                        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>entityProp <span style="color:blue;">in </span>entityProperties)
                        {
                            <span style="color:blue;">if </span>(entityProp.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(RelatedEnd))) <span style="color:green;">//Collection Property
                            </span>{
                                <span style="color:blue;">#region </span>Detail Reference - Add on &quot;* to 1&quot; relationship
                                <span style="color:#2b91af;">Type </span>detail = entityProp.PropertyType.GetGenericArguments().First();
                                <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= detail)
                                {
                                    AddToDictionary(objectCollection, entityType, detail);
                                }<span style="color:green;">//end if
                                </span><span style="color:blue;">#endregion
                            </span>}
                            <span style="color:blue;">else if </span>(entityProp.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(EntityReference)))
                            {
                                <span style="color:blue;">#region </span>Master referance - Add on &quot;1 to 1&quot; relationship
                                <span style="color:blue;">bool </span>addInDictionary = <span style="color:blue;">false</span>;
                                <span style="color:#2b91af;">Type </span>master = entityProp.PropertyType.GetGenericArguments().First();
                                <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= master)
                                {
                                    <span style="color:blue;">try
                                    </span>{
                                        <span style="color:#2b91af;">IEnumerable</span>&lt;PropertyInfo&gt; props = master.GetProperties().Where(p =&gt; p.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(EntityReference)));
                                        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= props &amp;&amp; props.Count() &gt; 0)
                                        {
                                            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>p <span style="color:blue;">in </span>props)
                                            {
                                                <span style="color:blue;">if </span>(p.PropertyType.GetGenericArguments().FirstOrDefault().Equals(entityType)
                                                     &amp;&amp; p.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(EntityReference))
                                                     &amp;&amp; !IsInDictionary(objectCollection, master, entityType))
                                                {
                                                    addInDictionary = <span style="color:blue;">true</span>;
                                                }
                                            }
                                        }
                                    }
                                    <span style="color:blue;">catch </span>(System.Reflection.<span style="color:#2b91af;">TargetException </span>ex)
                                    {
                                        <span style="color:blue;">throw </span>ErrorHandling.ErrorHandler.WrapException(ex, ErrorCodes.DB_EntityRelationship_Error_Code);
                                    }
                                    <span style="color:blue;">catch </span>(TargetInvocationException ex)
                                    {
                                        <span style="color:blue;">throw </span>ErrorHandling.ErrorHandler.WrapException(ex, ErrorCodes.DB_EntityRelationship_Error_Code);
                                    }
                                    <span style="color:blue;">if </span>(addInDictionary)
                                    {
                                        AddToDictionary(objectCollection, entityType, master);
                                    }
                                }<span style="color:green;">//end if
                                </span><span style="color:blue;">#endregion
                            </span>}
                        }
                    }<span style="color:green;">//end try
                    </span><span style="color:blue;">catch </span>(ReflectionTypeLoadException ex)
                    {
                        <span style="color:blue;">throw </span>ErrorHandling.ErrorHandler.WrapException(ex, ErrorCodes.DB_EntityRelationship_Error_Code);
                    }
                    <span style="color:blue;">catch </span>(EntityException ex)
                    {
                        <span style="color:blue;">throw </span>ErrorHandling.ErrorHandler.WrapException(ex, ErrorCodes.DB_EntityRelationship_Error_Code);
                    }
                }<span style="color:green;">//end if 

            </span>}<span style="color:green;">//end if
        </span>}<span style="color:green;">//end foreach
        //return the dictionary od priority list
        </span><span style="color:blue;">return </span>objectCollection;
    }
    <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception </span>ex)
    {
        <span style="color:blue;">throw </span>ErrorHandling.ErrorHandler.WrapException(ex, ErrorCodes.DB_EntityRelationship_Error_Code);
    }
}

<span style="color:blue;">#region </span>Private Methods
<span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Add Value into into Dictionary on checking old data
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;param name=&quot;Dictionary&quot;&gt;</span><span style="color:green;">Dictionary</span><span style="color:gray;">&lt;/param&gt;
/// &lt;param name=&quot;Key&quot;&gt;</span><span style="color:green;">Key</span><span style="color:gray;">&lt;/param&gt;
/// &lt;param name=&quot;Value&quot;&gt;</span><span style="color:green;">Value</span><span style="color:gray;">&lt;/param&gt;
</span><span style="color:blue;">private static void </span>AddToDictionary(<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt; Dictionary, <span style="color:#2b91af;">Type </span>Key, <span style="color:#2b91af;">Type </span>Value)
{
    <span style="color:blue;">if </span>(Dictionary.ContainsKey(Key))
    {
        <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt; prevValue = Dictionary[Key] <span style="color:blue;">as </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;;
        <span style="color:blue;">if </span>(prevValue != <span style="color:blue;">null </span>&amp;&amp; <span style="color:blue;">null </span>!= Value)
        {
            prevValue.Add(Value);
            Dictionary[Key] = prevValue;
        }<span style="color:green;">//end if
    </span>}
    <span style="color:blue;">else
    </span>{<span style="color:green;">//add new entry with default priority
        </span><span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= Value)
        {
            Dictionary.Add(Key, <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;() { Value });
        }
        <span style="color:blue;">else
        </span>{
            Dictionary.Add(Key, <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;());
        }
    }<span style="color:green;">//end else
</span>}
<span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Is in dictionary ?
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;param name=&quot;Dictionary&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color:blue;">private static bool </span>IsInDictionary(<span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt; Dictionary, <span style="color:#2b91af;">Type </span>key, <span style="color:#2b91af;">Type </span>value)
{
    <span style="color:blue;">if </span>(Dictionary.ContainsKey(key))
    {
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>item <span style="color:blue;">in </span>(Dictionary[key] <span style="color:blue;">as </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;))
        {
            <span style="color:blue;">if </span>(item.Equals(value))
            {
                <span style="color:blue;">return true</span>;
            }
        }
    }
    <span style="color:blue;">return false</span>;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><font face="Courier New"></font></p>
<p><font face="Consolas" size="2"></font></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">Now I <span>&#160;</span>was going to<span>&#160; </span>Sort this Using an Sorting algorithm , To make that First I <span>&#160;</span>declared a enum – </span></p>
<pre class="code">[<span style="color:#2b91af;">Flags</span>]
<span style="color:blue;">public enum </span><span style="color:#2b91af;">EnitySetRefType
</span>{
    None = 0x00,
    ReferencedBy = 0x01,
    RefAndRefBy = 0x02,
    References = 0x04
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><font face="Consolas" size="2"></font></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">And make a Wrapper class</span><span style="font-size:10pt;line-height:115%;font-family:&#39;">&#160; Of EntitySet<font face="Courier New">&#160;</font></span><span style="font-size:10pt;line-height:115%;font-family:consolas;">Like this -</span></p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Wrapper class of EntitySet
</span><span style="color:gray;">/// &lt;/summary&gt;
</span><span style="color:blue;">public class </span><span style="color:#2b91af;">EntitySetTypeWrapper
</span>{
    <span style="color:blue;">#region </span>Data Members

    <span style="color:green;">// member variables
    </span><span style="color:blue;">public static int </span>MaxLen;
    <span style="color:blue;">public static readonly </span><span style="color:#2b91af;">SortedList</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">EntitySetTypeWrapper</span>&gt; sortedList = <span style="color:blue;">new </span><span style="color:#2b91af;">SortedList</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">EntitySetTypeWrapper</span>&gt;();

    <span style="color:green;">// Instance variables
    </span><span style="color:blue;">private </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; _referencedBy = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;();
    <span style="color:blue;">private </span><span style="color:#2b91af;">EnitySetRefType </span>_refType;
    <span style="color:blue;">private string </span>_name;

    <span style="color:blue;">#endregion

    #region </span>Properites

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get/Set for ReferencedBy
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; ReferencedBy
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>(_referencedBy); }
        <span style="color:blue;">set </span>{ _referencedBy = <span style="color:blue;">value</span>; }
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get/Set for RefType
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:blue;">public </span><span style="color:#2b91af;">EnitySetRefType </span>RefType
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>(_refType); }
        <span style="color:blue;">set </span>{ _refType = <span style="color:blue;">value</span>; }
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Get/Set for Name
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:blue;">public string </span>Name
    {
        <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>(_name); }
        <span style="color:blue;">set </span>{ _name = <span style="color:blue;">value</span>; }
    }

    <span style="color:blue;">#endregion

    #region </span>Constructor

    <span style="color:gray;">/// &lt;summary&gt;
    ///
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;row&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">private </span>EntitySetTypeWrapper(<span style="color:blue;">string </span>_name)
    {
        Name = _name;
        <span style="color:blue;">if </span>(Name.Length &gt; MaxLen)
            MaxLen = Name.Length;

        AddRefType(<span style="color:#2b91af;">EnitySetRefType</span>.None);
    }

    <span style="color:blue;">#endregion

    #region </span>Methods - Private
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Private Method
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;referenceType&quot;&gt;&lt;/param&gt;
    </span><span style="color:blue;">private void </span>AddRefType(<span style="color:#2b91af;">EnitySetRefType </span>referenceType)
    {
        <span style="color:blue;">if </span>(_refType == <span style="color:#2b91af;">EnitySetRefType</span>.RefAndRefBy)
            <span style="color:blue;">return</span>;

        _refType |= referenceType;

        <span style="color:blue;">if </span>(_refType == (<span style="color:#2b91af;">EnitySetRefType</span>.ReferencedBy | <span style="color:#2b91af;">EnitySetRefType</span>.References))
            _refType = <span style="color:#2b91af;">EnitySetRefType</span>.RefAndRefBy;
    }
    <span style="color:blue;">#endregion
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p><span style="font-size:10pt;line-height:115%;font-family:consolas;"></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">Now What I have to do , I should make a Comparer class to sort the entity set Using this wrapper class . So I make a class named <span style="color:#2b91af;">EntityInfoComparer<span>&#160; </span></span>which is inherited from <span style="color:#2b91af;">IComparer</span>&lt;<span style="color:#2b91af;">EntitySetTypeWrapper</span>&gt;</span></p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">EntityInfoComparer </span>: <span style="color:#2b91af;">IComparer</span>&lt;EntitySetTypeWrapper&gt;
{
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Compare Two Entity
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;x&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;y&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">public int </span>Compare(EntitySetTypeWrapper x, EntitySetTypeWrapper y)
    {
        <span style="color:green;">// Invalid Data
        </span><span style="color:blue;">if </span>((x == <span style="color:blue;">null</span>) || (y == <span style="color:blue;">null</span>))
            <span style="color:blue;">throw new </span><span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Invalid data type&quot;</span>);

        <span style="color:green;">// Objects are the Same
        </span><span style="color:blue;">if </span>(x == y)
        {
            <span style="color:blue;">return </span>(0);
        }

        <span style="color:green;">// Sort by Category Number - Object X is in a LOWER Categroy than Object Y
        </span><span style="color:blue;">if </span>(x.RefType &lt; y.RefType)
            <span style="color:blue;">return </span>(-1);

        <span style="color:green;">// Sort by Category Number - Object X is in a HIGHER Categroy than Object Y
        </span><span style="color:blue;">if </span>(x.RefType &gt; y.RefType)
            <span style="color:blue;">return </span>(1);

        <span style="color:green;">// Sort by Category Number - Object X and Object Y are in the SAME Category
        // Is this a Category 3
        </span><span style="color:blue;">if </span>(x.RefType == <span style="color:#2b91af;">EnitySetRefType</span>.RefAndRefBy)
        {
            <span style="color:blue;">if </span>(IsInReferenceList(y.Name, x))
            {
                <span style="color:blue;">return </span>(-1);
            }

            <span style="color:blue;">if </span>(IsInReferenceList(x.Name, y))
            {
                <span style="color:blue;">return </span>(1);
            }
        }

        <span style="color:green;">// As a last resort sort by name
        </span><span style="color:blue;">return </span>CompareName(x, y);
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Compare Two Entity
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;x&quot;&gt;</span><span style="color:green;">First Entity Defination</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;y&quot;&gt;</span><span style="color:green;">Second Entity Defination</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">private static int </span>CompareName(EntitySetTypeWrapper x, EntitySetTypeWrapper y)
    {
        <span style="color:blue;">return </span>(x.Name.CompareTo(y.Name));
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Is in Reference List?
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;entityName&quot;&gt;</span><span style="color:green;">Name of Entity</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;entityInfo&quot;&gt;</span><span style="color:green;">Entity Defination</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">private static bool </span>IsInReferenceList(<span style="color:blue;">string </span>entityName, EntitySetTypeWrapper entityInfo)
    {
        <span style="color:green;">// Nothing to compare
        </span><span style="color:blue;">if </span>(entityInfo.ReferencedBy.Count == 0)
            <span style="color:blue;">return </span>(<span style="color:blue;">false</span>);

        <span style="color:green;">// This Entity is Referenced By Entity
        </span><span style="color:blue;">if </span>(entityInfo.ReferencedBy.Contains(entityName))
            <span style="color:blue;">return </span>(<span style="color:blue;">true</span>);

        <span style="color:green;">// Recursive call
        </span><span style="color:blue;">foreach </span>(<span style="color:blue;">string </span>childEntityName <span style="color:blue;">in </span>entityInfo.ReferencedBy)
        {
            <span style="color:green;">// Looking for a child of this entity
            </span><span style="color:blue;">if </span>(IsInReferenceList(entityName, EntitySetTypeWrapper.sortedList[childEntityName]))
                <span style="color:blue;">return </span>(<span style="color:blue;">true</span>);
        }

        <span style="color:blue;">return </span>(<span style="color:blue;">false</span>);
    }
}</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">So , Going to sort using the above dictionary and wrapper class of entity set.So I am going to write an static mathod who First make a dictionary using the method GetCurrentDBEntityRelationshipChildren and then make a <span style="color:#2b91af;">SortedList</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">EntitySetTypeWrapper</span>&gt; using the dictionary contents and at the end I sort the SortedList using EntityInfoComparer.</span></p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Create a sorted EntitySet list
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color:blue;">public static </span><span style="color:#2b91af;">List</span>&lt;EntitySetTypeWrapper&gt; CreateSortedEntityListOnChildFirst&lt;T&gt;() <span style="color:blue;">where </span>T : ObjectContext
{
    <span style="color:green;">// Retrieve Tables and Foreign Key Relationships
    //DataSet dataSet = GetDatabaseTableInformation();
    </span><span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Type</span>, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;&gt; entries = GenericDataAccessHelper.GetCurrentDBEntityRelationshipChildren&lt;T&gt;();

    sortedList.Clear();
    <span style="color:green;">// Add All Tables to a List
    </span><span style="color:blue;">foreach </span>(<span style="color:#2b91af;">Type </span>key <span style="color:blue;">in </span>entries.Keys)
    {
        EntitySetTypeWrapper tableInfo = <span style="color:blue;">new </span>EntitySetTypeWrapper(key.Name);
        sortedList.Add(tableInfo.Name, tableInfo);
    }

    <span style="color:green;">// Find Reference Table Information For Each Table
    </span><span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>item <span style="color:blue;">in </span>entries)
    {
        EntitySetTypeWrapper entityInfo = sortedList[(item.Key <span style="color:blue;">as </span><span style="color:#2b91af;">Type</span>).Name];

        <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt; valueRange = item.Value <span style="color:blue;">as </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Type</span>&gt;;
        <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= valueRange &amp;&amp; valueRange.Count &gt; 0 &amp;&amp; entityInfo != <span style="color:blue;">null</span>)
        {
            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>value <span style="color:blue;">in </span>valueRange)
            {
                <span style="color:blue;">string </span>childTable = value.Name;

                <span style="color:blue;">if </span>(childTable.Equals(entityInfo.Name))
                    <span style="color:blue;">continue</span>;

                <span style="color:green;">// Add Child Table to Current Table
                </span><span style="color:blue;">if </span>(!entityInfo.ReferencedBy.Contains(childTable))
                {
                    entityInfo.ReferencedBy.Add(childTable);

                    <span style="color:green;">// Add Reference Type Enumeration For Both Referenced and Referenced By Tables
                    </span>entityInfo.AddRefType(EnitySetRefType.ReferencedBy);
                    sortedList[childTable].AddRefType(EnitySetRefType.References);
                }
            }<span style="color:green;">//end foreach
        </span>}
        <span style="color:green;">// Sort Referenced By Tables By Name
        </span>entityInfo.ReferencedBy.Sort();
    }
    <span style="color:green;">// Perform the Sort with the Custom Sorter
    </span><span style="color:#2b91af;">List</span>&lt;EntitySetTypeWrapper&gt; list = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;EntitySetTypeWrapper&gt;(sortedList.Values);
    list.Sort(<span style="color:blue;">new </span>EntityInfoComparer());

    <span style="color:blue;">return </span>(list);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">That’s the end of<span>&#160; </span>story here…Still its working good with the Domain models of Entity set .&#160; Hope it will help you also. Feel free if you wanna comment on this.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=108&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/06/05/sorted-entityset-according-to-parent-child-relationship/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing Audit Trail using Entity Framework : Part -2</title>
		<link>http://morshedanwar.wordpress.com/2009/04/01/implementing-audit-trail-using-entity-framework-part-2/</link>
		<comments>http://morshedanwar.wordpress.com/2009/04/01/implementing-audit-trail-using-entity-framework-part-2/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 06:53:25 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/04/01/implementing-audit-trail-using-entity-framework-part-2/</guid>
		<description><![CDATA[&#160; In its&#160;part 1, I have talked about creating audit trail objects using the object state entries. As I said before In Second Part I will talk about roll back feature of this audit trail. In&#160;first part, I have said that, to get the Rollback feature from that audit trail we have to consider some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=81&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
<p><a href="http://morshedanwar.files.wordpress.com/2009/04/databasedbaudit1.jpg"><img title="DataBaseDbAudit" style="display:inline;border-width:0;" height="184" alt="DataBaseDbAudit" src="http://morshedanwar.files.wordpress.com/2009/04/databasedbaudit-thumb1.jpg?w=314&#038;h=184" width="314" border="0" /></a>&#160; </p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
<p><a href="http://morshedanwar.files.wordpress.com/2009/04/dbauditdata2.jpg"><img title="DBAuditData" style="display:inline;border-width:0;" height="218" alt="DBAuditData" src="http://morshedanwar.files.wordpress.com/2009/04/dbauditdata-thumb2.jpg?w=804&#038;h=218" width="804" border="0" /></a> </p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:black;line-height:115%;font-family:consolas;">In its</span></span><span class="apple-converted-space"><span style="font-size:9pt;color:black;line-height:115%;font-family:consolas;">&#160;</span></span><span class="apple-style-span"><span style="font-size:9pt;color:black;line-height:115%;font-family:consolas;"><a title="Implementing Audit Trail using Entity Framework Part -1" href="http://www.codeproject.com/KB/database/www.codeproject.com/KB/database/ImplAudingTrailUsingEFP1.aspx"><font color="#0000ff">part 1</font></a>, I have talked about creating audit trail objects using the object state entries. As I said before In Second Part I will talk about roll back feature of this audit trail. In</span></span><span class="apple-converted-space"><span style="font-size:9pt;color:black;line-height:115%;font-family:consolas;">&#160;</span></span><span class="apple-style-span"><span style="font-size:9pt;color:black;line-height:115%;font-family:consolas;"><a title="Implementing Audit Trail using Entity Framework Part -1" href="http://www.codeproject.com/KB/database/www.codeproject.com/KB/database/ImplAudingTrailUsingEFP1.aspx"><font color="#0000ff">first part</font></a>, I have said that, to get the Rollback feature from that audit trail we have to consider some issues.&#160; We have to maintain the order of entity graph while insertion and deletion. That means root entity has been inserted before the children and during deletion we have to make it reverse. The most important issue is that we have to make sure that each audit trail entry has been inserted according this order.</span></span><span style="font-size:9pt;line-height:115%;font-family:consolas;"> <span class="apple-style-span"><span style="color:#333333;">Other issues </span></span>
</p>
<p>   </span></p>
</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"><span>1.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">We have to maintain insertion date-time to sort the audit trial. </span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span>2.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Store the old data and new data. </span></span><span class="apple-style-span"><span style="font-size:9pt;line-height:115%;font-family:consolas;"></span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span>3.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Store the state the data as audit action.</span></span><span class="apple-style-span"><span style="font-size:9pt;line-height:115%;font-family:consolas;"> </span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
<p>&#160;</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">If user chooses a certain time to roll back, we have to start with last audits and do the roll back action for each audit till the certain time. So now the question is arises that what will be roll back action. You can guess that it’s depending on the Audit action of each entity. During rollback – </span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"><span>1.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Inserted data will be deleted. </span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"><span>2.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Deleted data will be inserted. </span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpLast" style="text-indent:-.25in;margin:0 0 10pt .5in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"><span>3.<span style="font:7pt &quot;">&#160; </span></span></span></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Modified data will replaced by the old one. </span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt .25in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">If we see to conceptual model, our Entity set for Audit was-</span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt .25in;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
<p><a href="http://morshedanwar.files.wordpress.com/2009/04/dbauditef1.jpg"><img title="dbAuditEF" style="display:inline;border-width:0;" height="244" alt="dbAuditEF" src="http://morshedanwar.files.wordpress.com/2009/04/dbauditef-thumb1.jpg?w=168&#038;h=244" width="168" border="0" /></a> </p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt .25in;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Here “RevisionStamp” holds the audit insertion date-time, Actions holds the audit action of the entity object (Insert/deleted/modified).Other properties describe themselves with their names. </span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">So here “Deleted” and “Modified” object will be roll backed with “Old data” property. And Inserted data will be roll backed with “New data” property. </span></span>    </p>
</p>
</p>
<p class="MsoNormal" style="margin:0 0 10pt .25in;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
</p>
</p>
<p class="MsoListParagraphCxSpFirst" style="margin:0;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"></span></span>    </p>
<p>&#160;<a href="http://morshedanwar.files.wordpress.com/2009/04/userdateinput1.jpg"><img title="UserDateInput" style="display:inline;border-width:0;" height="293" alt="UserDateInput" src="http://morshedanwar.files.wordpress.com/2009/04/userdateinput-thumb1.jpg?w=396&#038;h=293" width="396" border="0" /></a> </p>
</p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;"><span>&#160;&#160; </span>So First I am going to retrieve all the audits those happen after a specific date-time. This specific date-time is taken from the user. What we have to do, do rollback action for each audit till to specific date and we have to start with last one(bottom<span>&#160; </span>to Top).That is why we are going to sort the audit with “RevisionStamp” in a descending order and start doing rollback- </span></span>    </p>
</p>
</p>
<blockquote><p class="MsoListParagraphCxSpFirst" style="line-height:normal;margin:0;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;">
<pre class="code"><span style="color:blue;">public static bool </span>RollBack(<span style="color:#2b91af;">DateTime </span>rollBackDate, <span style="color:blue;">string </span>userName)
{
    <span style="color:blue;">using </span>(TransactionScope transactionScope = <span style="color:blue;">new </span>TransactionScope(System.Transactions.TransactionScopeOption.Required, <span style="color:#2b91af;">TimeSpan</span>.FromMinutes(30)))
    {
        AdventureWorksEntities context = <span style="color:blue;">new </span>AdventureWorksEntities();
        <span style="color:blue;">try
        </span>{
            <span style="color:blue;">if </span>(context.Connection.State != ConnectionState.Open)
            {<span style="color:green;">//Open the connection
                </span>context.Connection.Open();
            }<span style="color:green;">//end if
            //find all audits to roll back
            </span><span style="color:#2b91af;">IEnumerable</span>&lt;DBAudit&gt; auditsToRollBack = <span style="color:#2b91af;">DataAdapter</span>.GetEntity&lt;DBAudit&gt;(context, a =&gt; a.RevisionStamp &gt;= rollBackDate);

            <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= auditsToRollBack &amp;&amp; auditsToRollBack.Count() &gt; 0)
            {<span style="color:green;">//if any data found to roll back
                //Order by the audits by creational datetime
                </span><span style="color:#2b91af;">IEnumerable</span>&lt;DBAudit&gt; orderedAudits = auditsToRollBack.OrderByDescending(a =&gt; a.RevisionStamp);

                <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>audit <span style="color:blue;">in </span>orderedAudits)
                {<span style="color:green;">//iterate each audit
                    </span>AuditTrailHelper.DoRollBackAction(<span style="color:blue;">ref </span>context, audit, userName);
                }<span style="color:green;">//end foreach
                </span><span style="color:blue;">int </span>i = context.SaveChanges();
                <span style="color:blue;">if </span>(i &gt; 0)
                {
                    transactionScope.Complete();
                    <span style="color:blue;">return true</span>;
                }
            }
        }
        <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception </span>ex)
        {
            <span style="color:blue;">throw </span>ex;
        }
        <span style="color:blue;">finally
        </span>{
            <span style="color:green;">// Explicitly dispose of the context,
            // which closes the connection.
            </span>context.Dispose();
        }
    }
    <span style="color:blue;">return false</span>;
}</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;line-height:115%;font-family:consolas;">In DoRollBackAction as I said before, </span><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">“Deleted” and “Modified” object will be roll backed with “Old data” property. And Inserted data will be roll backed with “New data” property. </span></span></p>
</p>
</blockquote>
</p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 10pt;"><span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">As I stored old data and new data with XML Serialization while creating Audit object, I have to desterilize those data into “EntityObject” and do the reverse action according to the audit action. </span></span></p>
</p>
</p>
<blockquote>
<pre class="code"><span style="color:blue;">private static void </span>DoRollBackAction(<span style="color:blue;">ref </span>AdventureWorksEntities context,  DBAudit auditInfo, <span style="color:blue;">string </span>userName)
        {
            <span style="color:blue;">if </span>(auditInfo.Actions == AuditTrailHelper.AuditActions.U.ToString() || auditInfo.Actions ==AuditTrailHelper.AuditActions.D.ToString())
            {<span style="color:green;">//For action of Update and Delete
                //Deserialized old data from the XML
                </span><span style="color:blue;">object </span>oldData = AuditTrailHelper.GetAuditObjectFromXML(auditInfo.OldData, auditInfo.TableName);

                <span style="color:blue;">if </span>(oldData <span style="color:blue;">is </span>EntityObject)
                {
                    EntityObject oldEntity = (EntityObject)oldData;
                    oldEntity.EntityKey = <span style="color:blue;">null</span>;
                    <span style="color:green;">//add in case of delete or edit the current one with old data
                    </span><span style="color:#2b91af;">DataAdapter</span>.EditEntity(<span style="color:blue;">ref </span>context, oldEntity);
                }
            }
            <span style="color:blue;">else if </span>(auditInfo.Actions == AuditTrailHelper.AuditActions.I.ToString())
            {<span style="color:green;">//For Insert Action
                </span><span style="color:blue;">object </span>newData = AuditTrailHelper.GetAuditObjectFromXML(auditInfo.NewData, auditInfo.TableName);

                <span style="color:blue;">if </span>(newData <span style="color:blue;">is </span>EntityObject)
                {
                    <span style="color:green;">//Otherwise, delete the entity that has been inserted before
                    </span>EntityObject newEntity = (EntityObject)newData;
                    newEntity.EntityKey = <span style="color:blue;">null</span>;
                    EntityKey key = context.CreateEntityKey(newEntity.GetType().Name, newEntity);

                    <span style="color:blue;">object </span>objToRemoved = <span style="color:blue;">null</span>;
                    <span style="color:blue;">if </span>(context.TryGetObjectByKey(key, <span style="color:blue;">out </span>objToRemoved))
                    {<span style="color:green;">//delete the entity
                        </span>context.DeleteObject(objToRemoved);
                    }<span style="color:green;">//end if
                </span>}
            }
            <span style="color:green;">//delete the audit
            </span>context.DeleteObject(auditInfo);
        }
    }</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160; </span></span></p>
</p>
<p>    <span class="apple-style-span"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">After serializing the entity Object we get the XML string like this &#8211; </span></span></p>
</blockquote>
</p>
<p class="MsoNormalCxSpFirst" style="line-height:normal;margin:auto auto 0;"><span style="font-size:9pt;color:blue;font-family:consolas;"></span></p>
<h1><strong><span style="font-size:9pt;color:blue;font-family:consolas;"></span></strong></h1>
<blockquote>
<p><strong><span style="font-size:9pt;color:blue;line-height:115%;font-family:consolas;"></span></strong></p>
<pre class="code"><span style="color:blue;">&lt;</span><span style="color:#a31515;">SalesOrderDetail </span><span style="color:red;">xmlns:xsi</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.w3.org/2001/XMLSchema-instance</span>&quot; <span style="color:red;">xmlns:xsd</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.w3.org/2001/XMLSchema</span>&quot;<span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">EntityKey</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">EntitySetName</span><span style="color:blue;">&gt;</span>SalesOrderDetail<span style="color:blue;">&lt;/</span><span style="color:#a31515;">EntitySetName</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">EntityContainerName</span><span style="color:blue;">&gt;</span>AdventureWorksEntities<span style="color:blue;">&lt;/</span><span style="color:#a31515;">EntityContainerName</span><span style="color:blue;">&gt;
  &lt;/</span><span style="color:#a31515;">EntityKey</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">SalesOrderID</span><span style="color:blue;">&gt;</span>1<span style="color:blue;">&lt;/</span><span style="color:#a31515;">SalesOrderID</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">SalesOrderDetailID</span><span style="color:blue;">&gt;</span>0<span style="color:blue;">&lt;/</span><span style="color:#a31515;">SalesOrderDetailID</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">OrderQty</span><span style="color:blue;">&gt;</span>1<span style="color:blue;">&lt;/</span><span style="color:#a31515;">OrderQty</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">UnitPrice</span><span style="color:blue;">&gt;</span>1898.0944<span style="color:blue;">&lt;/</span><span style="color:#a31515;">UnitPrice</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">UnitPriceDiscount</span><span style="color:blue;">&gt;</span>0<span style="color:blue;">&lt;/</span><span style="color:#a31515;">UnitPriceDiscount</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">LineTotal</span><span style="color:blue;">&gt;</span>0<span style="color:blue;">&lt;/</span><span style="color:#a31515;">LineTotal</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">rowguid</span><span style="color:blue;">&gt;</span>6bfaa372-292a-4fd6-84d3-c4e900f09589<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rowguid</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">ModifiedDate</span><span style="color:blue;">&gt;</span>2009-03-26T16:16:41.9691358+06:00<span style="color:blue;">&lt;/</span><span style="color:#a31515;">ModifiedDate</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">SalesOrderHeaderReference </span><span style="color:blue;">/&gt;
  &lt;</span><span style="color:#a31515;">SpecialOfferProductReference</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">EntityKey</span><span style="color:blue;">&gt;
      &lt;</span><span style="color:#a31515;">EntitySetName</span><span style="color:blue;">&gt;</span>SpecialOfferProduct<span style="color:blue;">&lt;/</span><span style="color:#a31515;">EntitySetName</span><span style="color:blue;">&gt;
      &lt;</span><span style="color:#a31515;">EntityContainerName</span><span style="color:blue;">&gt;</span>AdventureWorksEntities<span style="color:blue;">&lt;/</span><span style="color:#a31515;">EntityContainerName</span><span style="color:blue;">&gt;
      &lt;</span><span style="color:#a31515;">EntityKeyValues</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">EntityKeyMember</span><span style="color:blue;">&gt;
          &lt;</span><span style="color:#a31515;">Key</span><span style="color:blue;">&gt;</span>SpecialOfferID<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Key</span><span style="color:blue;">&gt;
          &lt;</span><span style="color:#a31515;">Value </span><span style="color:red;">xsi:type</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">xsd:int</span>&quot;<span style="color:blue;">&gt;</span>1<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:blue;">&gt;
        &lt;/</span><span style="color:#a31515;">EntityKeyMember</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">EntityKeyMember</span><span style="color:blue;">&gt;
          &lt;</span><span style="color:#a31515;">Key</span><span style="color:blue;">&gt;</span>ProductID<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Key</span><span style="color:blue;">&gt;
          &lt;</span><span style="color:#a31515;">Value </span><span style="color:red;">xsi:type</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">xsd:int</span>&quot;<span style="color:blue;">&gt;</span>777<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:blue;">&gt;
        &lt;/</span><span style="color:#a31515;">EntityKeyMember</span><span style="color:blue;">&gt;
      &lt;/</span><span style="color:#a31515;">EntityKeyValues</span><span style="color:blue;">&gt;
    &lt;/</span><span style="color:#a31515;">EntityKey</span><span style="color:blue;">&gt;
  &lt;/</span><span style="color:#a31515;">SpecialOfferProductReference</span><span style="color:blue;">&gt;
&lt;/</span><span style="color:#a31515;">SalesOrderDetail</span><span style="color:blue;">&gt;</span></pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
<p><span class="apple-style-span"><span style="font-size:9pt;color:black;font-family:consolas;">I have to deserialized this xml string.During deserializing, I have created a XML document and create an XML reader using that document.Using that reader I deserialized the entity object. The method to deserialize the entityobject is –</span></span><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160; </span></span></p>
</blockquote>
</p>
<blockquote>
<pre class="code"><span style="color:blue;">public static object </span>GetAuditObjectFromXML(<span style="color:blue;">string </span>ObjectInXML, <span style="color:blue;">string </span>typeName)
{
    XDocument doc = XDocument.Parse(ObjectInXML);
    <span style="color:green;">//Assuming doc is an XML document containing a serialized object and objType is a System.Type set to the type of the object.
    </span>XmlReader reader = doc.CreateReader();

    <span style="color:#2b91af;">Type </span>entityType = <span style="color:#2b91af;">Assembly</span>.GetExecutingAssembly().GetType(<span style="color:#a31515;">&quot;ImplAuditTrailUsingEF.&quot; </span>+ typeName);

    XmlSerializer ser = <span style="color:blue;">new </span>XmlSerializer(entityType);
    <span style="color:blue;">return </span>ser.Deserialize(reader);
}</pre>
</blockquote>
</p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;">For inserting deleted object or change back modifed object I wrote a single method to edit the object – “EditEntity(<span style="color:blue;">ref</span> <span style="color:#2b91af;">AdventureWorksEntities</span> context, <span style="color:#2b91af;">EntityObject</span> entity)”.Is this method I change the existance object and attch to the container otherwise I have inserted the entityObject into the context.To save all of this change we must need a entry in Object state cache. </span></p>
</p>
</p>
<p class="MsoNormalCxSpFirst" style="line-height:normal;margin:auto auto 0;"><span style="font-size:9pt;font-family:consolas;"><span></span></span></p>
<h1><span style="font-size:9pt;font-family:consolas;"><strong><span></span></strong></span></h1>
<p><span style="font-size:9pt;font-family:consolas;"><strong><span></span></strong></span></p>
<blockquote>
<pre class="code"><span style="color:blue;">public static void </span>EditEntity(<span style="color:blue;">ref </span>AdventureWorksEntities context, <span style="color:#2b91af;">EntityObject </span>entity)
{
    <span style="color:green;">// Define an ObjectStateEntry and EntityKey for the current object.
    </span><span style="color:#2b91af;">EntityKey </span>key;
    <span style="color:blue;">object </span>originalItem;
    <span style="color:green;">// Get the detached object’s entity key.
    </span><span style="color:blue;">if </span>(entity.EntityKey == <span style="color:blue;">null</span>)
    {
        <span style="color:green;">// Get the entity key of the updated object.
        </span>key = context.CreateEntityKey(entity.GetType().Name, entity);
    }
    <span style="color:blue;">else
    </span>{
        key = entity.EntityKey;
    }
    <span style="color:blue;">try
    </span>{
        <span style="color:green;">// Get the original item based on the entity key from the context
        // or from the database.
        </span><span style="color:blue;">if </span>(context.TryGetObjectByKey(key, <span style="color:blue;">out </span>originalItem))
        {<span style="color:green;">//accept the changed property
            // Call the ApplyPropertyChanges method to apply changes
            // from the updated item to the original version.
            </span>context.ApplyPropertyChanges(
                key.EntitySetName, entity);
        }
        <span style="color:blue;">else
        </span>{<span style="color:green;">//add the new entity
            </span>context.AddObject(entity.GetType().Name, entity);
        }<span style="color:green;">//end else
    </span>}
    <span style="color:blue;">catch </span>(System.Data.<span style="color:#2b91af;">MissingPrimaryKeyException </span>ex)
    {
        <span style="color:blue;">throw </span>ex;
    }
    <span style="color:blue;">catch </span>(System.Data.<span style="color:#2b91af;">MappingException </span>ex)
    {
        <span style="color:blue;">throw </span>ex;
    }
    <span style="color:blue;">catch </span>(System.Data.<span style="color:#2b91af;">DataException </span>ex)
    {
        <span style="color:blue;">throw </span>ex;
    }
}</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p></blockquote>
<p class="MsoListParagraphCxSpMiddle" style="margin:0;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">Now all we have to do is just call the “RollBack” method and give it a specific date to roll back. </span></p>
<pre class="code"><span style="color:blue;">private void </span>btnRollBack_Click(<span style="color:blue;">object </span>sender, RoutedEventArgs e)
{
    AuditTrailHelper.RollBack(dtpRollBackDate.SelectedDate.Value, <span style="color:#a31515;">&quot;Admin&quot;</span>);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
</p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 10pt;"><span style="font-size:9pt;color:#333333;line-height:115%;font-family:consolas;">That’s all for implementation of Audit trial with entity framework. Here I deleted the audits which has been rolling backed. Definitely each Roll Back operation is also a db operation and I do audit for each roll back operation.</span></p>
</p>
</p>
<p>You can get the SourceCode and the article also in codeproject site&#160; -&#160;&#160; <a href="http://www.codeproject.com/KB/database/ImplAudingTrailUsingEFP-2.aspx"><font color="#0000ff">http://www.codeproject.com/KB/database/ImplAudingTrailUsingEFP-2.aspx</font></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=81&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/04/01/implementing-audit-trail-using-entity-framework-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/04/databasedbaudit-thumb1.jpg" medium="image">
			<media:title type="html">DataBaseDbAudit</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/04/dbauditdata-thumb2.jpg" medium="image">
			<media:title type="html">DBAuditData</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/04/dbauditef-thumb1.jpg" medium="image">
			<media:title type="html">dbAuditEF</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/04/userdateinput-thumb1.jpg" medium="image">
			<media:title type="html">UserDateInput</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing Audit Trail using Ado.net Entity Framework : Part -1</title>
		<link>http://morshedanwar.wordpress.com/2009/03/26/implementing-audit-trail-using-adonet-entity-framework-part-1/</link>
		<comments>http://morshedanwar.wordpress.com/2009/03/26/implementing-audit-trail-using-adonet-entity-framework-part-1/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 12:18:18 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/03/26/implementing-audit-trail-using-adonet-entity-framework-part-1/</guid>
		<description><![CDATA[Entity framework keeps track for those entire objects and relationships which have been deleted, added and modified in the container. EF keeps the state of the object and holds the necessary change-information and all these track information for each object or relationship resides as “objectStateEntry”. Using “ObjectStateManager” one can access all these change-information like object-state [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=31&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:consolas;"><span style="font-size:small;">Entity framework keeps track for those entire objects and relationships which have been deleted, added and modified in the container. EF keeps the state of the object and holds the necessary change-information and all these track information for each object or relationship resides as “objectStateEntry”. Using “ObjectStateManager” one can access all these change-information like object-state (added/modified/deleted), modified properties, original and current values and can easily do audit trail for those objects. To get the Rollback feature from that audit trail we have to consider some issues. <span>&#160;</span>We have to maintain the order of entity graph while insertion and deletion. That means root entity has been inserted before the children and during deletion we have to make it reverse. The most important issue is that we have to make sure that audit trail entry will be inserted according this order. </span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:consolas;"><span style="font-size:small;"><span>&#160;</span>Here I am going to talk about audit trail implementation that’s capable to rollback to a certain period. To make such implementation I am going to use the Entity framework’s caching Management that is called as “ObjectStateManager”.Using this Manager I will be capable to find out the object that is currently changed or added or deleted and resides in EF cache as Object state entry. In part 1, I just going to talk about creating audit trail objects using the object state entry. In Second Part I will talk about roll back feature of this audit trial. </span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:consolas;"><span style="font-size:small;"><span>&#160;</span>First I make table audit trail in database </span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:small;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;">
<p><span style="font-size:small;">&#160;<a href="http://morshedanwar.files.wordpress.com/2009/03/databasedbaudit3.jpg"><img title="DataBaseDbAudit" style="display:inline;border-width:0;" height="224" alt="DataBaseDbAudit" src="http://morshedanwar.files.wordpress.com/2009/03/databasedbaudit-thumb3.jpg?w=384&#038;h=224" width="384" border="0" /></a> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:consolas;"><span style="font-size:small;">For this table, I am going to make an Entity set in my conceptual level as </span></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:small;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;">
<p><span style="font-size:small;">&#160;<a href="http://morshedanwar.files.wordpress.com/2009/03/dbauditef3.jpg"><img title="dbAuditEF" style="display:inline;border-width:0;" height="323" alt="dbAuditEF" src="http://morshedanwar.files.wordpress.com/2009/03/dbauditef-thumb3.jpg?w=222&#038;h=323" width="222" border="0" /></a> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:consolas;"><span style="font-size:small;">In Entity Framework, to save my all changes into Db we have call “Context.SaveChanges()” and This Context is a container that has been inherited<span>&#160; </span>from “ObjectContext” Class.To create the Audit trail<span>&#160; </span>Objects for each “Modified/Added/Deleted”I am going to catch the event </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:consolas;">Context.SavingChanges +=<span style="color:blue;">new</span> EventHandler(Context_SavingChanges); </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;">
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">In sample program I have done it with writing partial class of </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;color:blue;font-family:consolas;">public</span><span style="font-size:9pt;font-family:consolas;"> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">AdventureWorksEntities </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;color:blue;font-family:consolas;">partial</span><span style="font-size:9pt;font-family:consolas;"> <span style="color:blue;">void</span> OnContextCreated() </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">this</span>.SavingChanges += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(AdventureWorksEntities_SavingChanges); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">void</span> AdventureWorksEntities_SavingChanges(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>}</span><span style="font-size:9pt;line-height:115%;font-family:consolas;"> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">So in my <span style="color:#c00000;">“</span></span><span style="font-size:10pt;color:#c00000;line-height:115%;font-family:consolas;">AdventureWorksEntities_SavingChanges</span><span style="font-size:10pt;color:#c00000;line-height:115%;font-family:consolas;">”</span><span style="font-size:10pt;line-height:115%;font-family:consolas;"> method I am going to create all “dbaudit” objects those are going to save in DB.Here its takes each entry from EF cache of state- Added or Deleted or Modified and call a factory method to produce audit trail object. </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;background:white;font-family:consolas;"><span>&#160;&#160;&#160; </span></span><span style="font-size:9pt;color:blue;font-family:consolas;">public</span><span style="font-size:9pt;font-family:consolas;"> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">AdventureWorksEntities </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">public</span> <span style="color:blue;">string</span> UserName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; } </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DBAudit</span>&gt; auditTrailList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DBAudit</span>&gt;(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">public</span> <span style="color:blue;">enum</span> <span style="color:#2b91af;">AuditActions </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>I, </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>U, </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>D </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">partial</span> <span style="color:blue;">void</span> OnContextCreated() </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">this</span>.SavingChanges += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(AdventureWorksEntities_SavingChanges); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">void</span> AdventureWorksEntities_SavingChanges(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">ObjectStateEntry</span>&gt; changes = <span style="color:blue;">this</span>.ObjectStateManager.GetObjectStateEntries(<span style="color:#2b91af;">EntityState</span>.Added | <span style="color:#2b91af;">EntityState</span>.Deleted | <span style="color:#2b91af;">EntityState</span>.Modified); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">foreach</span> (<span style="color:#2b91af;">ObjectStateEntry</span> stateEntryEntity <span style="color:blue;">in</span> changes) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (!stateEntryEntity.IsRelationship &amp;&amp; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>stateEntryEntity.Entity != <span style="color:blue;">null</span> &amp;&amp; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>!(stateEntryEntity.Entity <span style="color:blue;">is</span> <span style="color:#2b91af;">DBAudit</span>)) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span>&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">//is a normal entry, not a relationship </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">DBAudit</span> audit = <span style="color:blue;">this</span>.AuditTrailFactory(stateEntryEntity, UserName); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>auditTrailList.Add(audit); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (auditTrailList.Count &gt; 0) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> audit <span style="color:blue;">in</span> auditTrailList) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">//add all audits </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">this</span>.AddToDBAudit(audit); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span><span style="font-size:9pt;color:black;font-family:consolas;">And here “</span></span><span><span style="font-size:9pt;color:#810001;font-family:consolas;">AuditTrailFactory</span></span><span><span style="font-size:9pt;color:black;font-family:consolas;">” is Factory method to create “dbaudit” object. In Entity Framework each entry holds all change definition. Especially for Modify state it keeps the modified properties. I have collected all these properties and serialized them as XML. Using this field you can easily show the changes of modified object with doing any comparison of old and new data.</span></span><span><span style="font-size:9pt;color:black;font-family:&quot;">&#160;</span></span><span style="font-size:10pt;font-family:consolas;"> </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">private</span> <span style="color:#2b91af;">DBAudit</span> AuditTrailFactory(<span style="color:#2b91af;">ObjectStateEntry</span> entry, <span style="color:blue;">string</span> UserName) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">DBAudit</span> audit = <span style="color:blue;">new</span> <span style="color:#2b91af;">DBAudit</span>(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.AuditId = <span style="color:#2b91af;">Guid</span>.NewGuid().ToString(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.RevisionStamp = <span style="color:#2b91af;">DateTime</span>.Now; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.TableName = entry.EntitySet.Name; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.UserName = UserName; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (entry.State == <span style="color:#2b91af;">EntityState</span>.Added) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">//entry is Added </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.NewData = GetEntryValueInString(entry, <span style="color:blue;">false</span>); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.Actions = <span style="color:#2b91af;">AuditActions</span>.I.ToString(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">else</span> <span style="color:blue;">if</span> (entry.State == <span style="color:#2b91af;">EntityState</span>.Deleted) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">//entry in deleted </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.OldData = GetEntryValueInString(entry, <span style="color:blue;">true</span>); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.Actions = <span style="color:#2b91af;">AuditActions</span>.D.ToString(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">else </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">//entry is modified </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.OldData = GetEntryValueInString(entry, <span style="color:blue;">true</span>); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.NewData = GetEntryValueInString(entry, <span style="color:blue;">false</span>); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.Actions = <span style="color:#2b91af;">AuditActions</span>.U.ToString(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:blue;">string</span>&gt; modifiedProperties = entry.GetModifiedProperties(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:green;">//assing collection of mismatched Columns name as serialized string </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>audit.ChangedColumns = <span style="color:#2b91af;">XMLSerializationHelper</span>.XmlSerialize(modifiedProperties.ToArray()); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">return</span> audit; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span><span style="font-size:10pt;color:black;font-family:consolas;">Here “</span></span><span><span style="font-size:10pt;color:#810001;font-family:consolas;">GetEntryValueInString</span></span><span><span style="font-size:10pt;color:black;font-family:consolas;">” is for creating XML text of Previous or modified object. First I made a clone the current object. Using</span></span><span><span style="font-size:10pt;color:black;font-family:consolas;"> “</span></span><span><span style="font-size:10pt;color:black;font-family:consolas;">entry.GetModifiedProperties()” I can get only modified properties of an object and Using “OriginalValues” and</span></span><span><span style="font-size:10pt;color:black;font-family:consolas;">&#160;</span></span><span><span style="font-size:10pt;color:black;font-family:consolas;">“CurrentValues” I can build myself the old data and new data. Factory has told me what that wants – old or new. At the End I have done XML serialized and return XML string.</span></span><span><span style="font-size:10pt;color:black;font-family:&quot;">&#160;</span></span><span style="font-size:10pt;"><span style="font-family:calibri;"> </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:consolas;"><span>&#160;&#160; </span></span><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160; </span><span style="color:blue;">private</span> <span style="color:blue;">string</span> GetEntryValueInString(<span style="color:#2b91af;">ObjectStateEntry</span> entry, <span style="color:blue;">bool</span> isOrginal) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (entry.Entity <span style="color:blue;">is</span> <span style="color:#2b91af;">EntityObject</span>) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">object</span> target = CloneEntity((<span style="color:#2b91af;">EntityObject</span>)entry.Entity); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> propName <span style="color:blue;">in</span> entry.GetModifiedProperties()) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">object</span> setterValue = <span style="color:blue;">null</span>; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (isOrginal) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:green;">//Get orginal value </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>setterValue = entry.OriginalValues[propName]; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">else </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:green;">//Get orginal value </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>setterValue = entry.CurrentValues[propName]; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:green;">//Find property to update </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">PropertyInfo</span> propInfo = target.GetType().GetProperty(propName); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:green;">//update property with orgibal value </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">if</span> (setterValue == <span style="color:#2b91af;">DBNull</span>.Value) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{<span style="color:green;">// </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>setterValue = <span style="color:blue;">null</span>; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>propInfo.SetValue(target, setterValue, <span style="color:blue;">null</span>); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>}<span style="color:green;">//end foreach </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">XmlSerializer</span> formatter = <span style="color:blue;">new</span> <span style="color:#2b91af;">XmlSerializer</span>(target.GetType()); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">XDocument</span> document = <span style="color:blue;">new</span> <span style="color:#2b91af;">XDocument</span>(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">using</span> (<span style="color:#2b91af;">XmlWriter</span> xmlWriter = document.CreateWriter()) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>formatter.Serialize(xmlWriter, target); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span>&#160;</span><span style="color:blue;">return</span> document.Root.ToString(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">return</span> <span style="color:blue;">null</span>; </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">To clone the entity I have used the method which I have found in MSDN forum Post </span><span style="font-size:small;"><span style="font-family:consolas;">(Thanx </span><span><span style="color:#0072bc;font-family:consolas;border-color:windowtext;border-width:1pt;padding:0;">Patrick Magee </span></span><span><span style="color:#0072bc;font-family:wingdings;border-color:windowtext;border-width:1pt;padding:0;"><span>J</span></span></span><span><span style="color:#0072bc;font-family:consolas;border-color:windowtext;border-width:1pt;padding:0;"> </span></span><span style="font-family:consolas;">)</span></span><span style="font-size:10pt;line-height:115%;font-family:consolas;">- </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">public</span> <span style="color:#2b91af;">EntityObject</span> CloneEntity(<span style="color:#2b91af;">EntityObject</span> obj) </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{ </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">DataContractSerializer</span> dcSer = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataContractSerializer</span>(obj.GetType()); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#2b91af;">MemoryStream</span> memoryStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>(); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>dcSer.WriteObject(memoryStream, obj); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>memoryStream.Position = 0; </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;">
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span>&#160;&#160; </span><span style="color:#2b91af;">EntityObject</span> newObject = (<span style="color:#2b91af;">EntityObject</span>)dcSer.ReadObject(memoryStream); </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:blue;">return</span> newObject; </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:9pt;line-height:115%;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:consolas;">That is all for Part-1 where I just create the Audit trail objects for each CUD operation.</span></p>
<p> <span style="font-size:10pt;line-height:115%;font-family:consolas;">
<p>You can get the SourceCode and the article also in codeproject site&#160; -&#160;&#160; <a href="http://www.codeproject.com/KB/database/ImplAudingTrailUsingEFP1.aspx"><font color="#0000ff">http://www.codeproject.com/KB/database/ImplAudingTrailUsingEFP1.aspx</font></a></p>
<p> </span>
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e85c1fc0-8b01-4f74-b4a2-a23a036ca7a2" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/Audit+Trail+Entity+Framework+Ado.net" rel="tag">Audit Trail Entity Framework Ado.net</a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=31&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/03/26/implementing-audit-trail-using-adonet-entity-framework-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/03/databasedbaudit-thumb3.jpg" medium="image">
			<media:title type="html">DataBaseDbAudit</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/03/dbauditef-thumb3.jpg" medium="image">
			<media:title type="html">dbAuditEF</media:title>
		</media:content>
	</item>
		<item>
		<title>Type conversion &#8211; Using Expression in place of reflection ( C#.net )</title>
		<link>http://morshedanwar.wordpress.com/2009/03/24/type-convertion-using-expression-in-place-of-reflection-cnet/</link>
		<comments>http://morshedanwar.wordpress.com/2009/03/24/type-convertion-using-expression-in-place-of-reflection-cnet/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 12:58:00 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Expression]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/03/24/type-convertion-using-expression-in-place-of-reflection-cnet/</guid>
		<description><![CDATA[Here I write a very simple method (you can make it more generic if you want) that get source object, target Type and a dictionary. First two parameters need not any explanation and the dictionary contains the mapping definition where the key holds the target type properties and value holds the source type properties. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=14&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="line-height:normal;margin:0 0 10pt;"><font size="3"><span style="color:#063e3f;font-family:consolas;">Here I write a very simple method (you can make it more generic if you want) that get source object, target Type and a dictionary. First two parameters need not any explanation and the dictionary contains the mapping definition where the key holds the target type properties and value holds the source type properties. If value is empty or null that means that should not be mapped (If you don’t want to map any particular property You can also delete the element from dictionary).&#160; </span><span style="font-family:consolas;"></span></font>    </p>
</p>
</p>
<pre class="code"><span style="color:blue;">private static object </span>ConvertTo(<span style="color:blue;">object </span>source, <span style="color:#2b91af;">Type </span>targetType, Dictionary propertyMapDictionary)
{
    <span style="color:green;">//create a instance of target object
    </span><span style="color:blue;">object </span>target = <span style="color:#2b91af;">Activator</span>.CreateInstance(targetType);

    <span style="color:blue;">if </span>(target != <span style="color:blue;">null </span>&amp;&amp; <span style="color:blue;">null </span>!= propertyMapDictionary &amp;&amp; propertyMapDictionary.Count &gt; 0)
    {
        <span style="color:green;">//determine the source type
        </span><span style="color:#2b91af;">Type </span>sourceType = source.GetType();
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>item <span style="color:blue;">in </span>propertyMapDictionary.Keys)
        {
            <span style="color:blue;">if </span>(propertyMapDictionary.ContainsKey(item)
            &amp;&amp; propertyMapDictionary[item] != <span style="color:blue;">null
            </span>&amp;&amp; propertyMapDictionary[item] != <span style="color:blue;">string</span>.Empty)
            {
                <span style="color:green;">//Initialize parameter expresssions for getter method
                </span>System.Linq.Expressions.<span style="color:#2b91af;">ParameterExpression </span>param = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">Object</span>), <span style="color:#a31515;">&quot;param&quot;</span>);
                System.Linq.Expressions.<span style="color:#2b91af;">Expression </span>convertedParam = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Convert(param, sourceType);
                System.Linq.Expressions.<span style="color:#2b91af;">LambdaExpression </span>GetPropertyValueExp = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Lambda(System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Property(convertedParam, propertyMapDictionary[item]), param);
                <span style="color:green;">//Create dynamic getter Delegate
                </span><span style="color:#2b91af;">Delegate </span>dynamicGetter = GetPropertyValueExp.Compile();

                <span style="color:green;">//Get property info target type
                </span><span style="color:#2b91af;">PropertyInfo </span>ITargetPInfo = targetType.GetProperty(item, <span style="color:#2b91af;">BindingFlags</span>.Public | <span style="color:#2b91af;">BindingFlags</span>.Instance);
                <span style="color:green;">//Determmine set method
                </span><span style="color:#2b91af;">MethodInfo </span>SetterMethodInfo = ITargetPInfo.GetSetMethod();
                <span style="color:green;">//Initialize the parameter- expressions for setter method
                </span>System.Linq.Expressions.<span style="color:#2b91af;">ParameterExpression </span>paramo = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Parameter(targetType, <span style="color:#a31515;">&quot;param&quot;</span>);
                System.Linq.Expressions.<span style="color:#2b91af;">ParameterExpression </span>parami = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Parameter(ITargetPInfo.PropertyType, <span style="color:#a31515;">&quot;newvalue&quot;</span>);
                System.Linq.Expressions.<span style="color:#2b91af;">MethodCallExpression </span>MethodCallSetterOfProperty = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Call(paramo, SetterMethodInfo, parami);
                System.Linq.Expressions.<span style="color:#2b91af;">LambdaExpression </span>SetPropertyValueExp = System.Linq.Expressions.<span style="color:#2b91af;">Expression</span>.Lambda(MethodCallSetterOfProperty, paramo, parami);
                <span style="color:green;">//Create a dynamic setter Delegate
                </span><span style="color:#2b91af;">Delegate </span>dynamicSetter = SetPropertyValueExp.Compile();
                <span style="color:green;">//Invoke getter method and get the source property value
                </span><span style="color:blue;">object </span>actualValue = dynamicGetter.DynamicInvoke(source);
                <span style="color:green;">//Invoke setter method and set the target property value
                </span>dynamicSetter.DynamicInvoke(target, System.<span style="color:#2b91af;">Convert</span>.ChangeType(actualValue, ITargetPInfo.PropertyType));
            }<span style="color:green;">//end if
        </span>}<span style="color:green;">//end foreach
    </span>}
    <span style="color:blue;">return </span>target;
}<span style="font-size:9pt;font-family:consolas;"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span></pre>
<p class="MsoNormal" style="line-height:normal;margin:0;">
</p>
</p>
<p>  <span style="color:#063e3f;font-family:consolas;"><font size="3">Its basically map only those properties that is found in dictionary (Mapping definition) and pick up value and set it to the target object .Here I didn’t do any datatype check for the mapped property. What I am trying to do here , I just use expression to fetch attribute value and set it to target object .</font></span></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3ff46f4e-b863-40a3-972e-faf9c8cdb1bb" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/Reflection+Expression+Tree" rel="tag">Reflection Expression Tree</a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=14&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/03/24/type-convertion-using-expression-in-place-of-reflection-cnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Generic method to query in Ado.net Entity Framework</title>
		<link>http://morshedanwar.wordpress.com/2009/03/24/generic-method-to-query-in-adonet-entity-framework/</link>
		<comments>http://morshedanwar.wordpress.com/2009/03/24/generic-method-to-query-in-adonet-entity-framework/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 12:54:00 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://morshedanwar.wordpress.com/2009/03/24/generic-method-to-query-in-adonet-entity-framework/</guid>
		<description><![CDATA[Here I am going to talk about the generic method and expression to query using Ado.net Entity Framework. I know it’s not a big deal but I choose simple thing to share my very little knowledge (!!) about expression and entity framework.Ok, here I consider an example of querying Max value. I know it’s already [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=15&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:10pt;line-height:115%;">
<p><font face="Calibri"></font></p>
<p class="MsoNormal" style="text-indent:7.5pt;margin:0 0 10pt;"><span style="color:#063e3f;font-family:consolas;"><font size="3">Here I am going to talk about the generic method and expression to query using Ado.net Entity Framework. I know it’s not a big deal but I choose simple thing to share my very little knowledge (!!) about expression and entity framework.Ok, here I consider an example of querying Max value. I know it’s already in there but it’s only an approach- of how you use your own (custom) expression when you are in generic place and&#160; you don’t know much (properties) about your entity set: </font></span>      </p>
</p>
</p>
<p class="MsoNormalCxSpMiddle" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;"><span style="font-size:9pt;color:#063e3f;font-family:consolas;"></span></p>
<p class="MsoNormalCxSpMiddle" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;">
<p class="MsoNormalCxSpMiddle" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;">
<p class="MsoNormalCxSpMiddle" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;">
<pre class="code"><span style="color:blue;">public static int </span>GetMaxValue  &lt;  T  &gt;  (<span style="color:#2b91af;">ObjectContext </span>contex, <span style="color:blue;">string </span>keyFieldName)
{
   <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= contex)
   {
     <span style="color:blue;">try</span>{
       <span style="color:green;">// First we define the parameter that we are going to use the clause.
        </span><span style="color:blue;">var </span>param = <span style="color:#2b91af;">Expression</span>.Parameter(<span style="color:blue;">typeof</span>(T), <span style="color:blue;">typeof</span>(T).Name);
      <span style="color:green;">// Now we’ll make our lambda function that returns the &quot;ID&quot; property .
       </span><span style="color:blue;">var </span>maxExpression = <span style="color:#2b91af;">Expression</span>.Lambda&lt; <span style="color:#2b91af;">Func </span>&lt;  T ,<span style="color:blue;">int  </span>&gt; &gt;(<span style="color:#2b91af;">Expression</span>.Property(param, keyFieldName), param);
       <span style="color:green;">// Now I can get desire entityset from context using reflection.
      </span>System.Data.Objects.<span style="color:#2b91af;">ObjectQuery  </span>&lt;  T &gt;    query = contex.GetType().GetProperties().Where(p =&gt; p.PropertyType.BaseType.Equals(<span style="color:blue;">typeof</span>(System.Data.Objects.<span style="color:#2b91af;">ObjectQuery</span>)) &amp;&amp; p.PropertyType.GetGenericArguments().First().Equals(<span style="color:blue;">typeof</span>(T))).First().GetValue(contex, <span style="color:blue;">null</span>) asSystem.Data.Objects.ObjectQuery &lt;  T &gt; ;
         <span style="color:blue;">if </span>(<span style="color:blue;">null </span>!= query)
            {
                <span style="color:blue;">int</span>? i = query.Max &lt;  T ,  <span style="color:blue;">int </span>&gt;(maxExpression);
                <span style="color:blue;">if </span>(i.HasValue)
                <span style="color:blue;">return </span>i.Value;
             }
       }
       <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception</span>){}
      }
     <span style="color:blue;">return </span>0;
}</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
<p class="MsoNormalCxSpMiddle" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;"><span style="color:#063e3f;font-family:consolas;"><font size="3">You can create your own expression in place of “maxExpression” . Now how do you call this generic method from generic place.lets consider that you want to get the Max of entity-key(Primary key) of an entity-set(Table) and set the (Max + 1) in your new entity object that is going to be added in database.Using reflection you can get the property name/names of Entity key </font></span></p>
</p>
</p>
<pre class="code"><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">PropertyInfo</span>&gt; keyProperties = entity.GetType().GetProperties().
    Where(p =&gt; ((<span style="color:#2b91af;">EdmScalarPropertyAttribute</span>[])p.GetCustomAttributes(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">EdmScalarPropertyAttribute</span>), <span style="color:blue;">false</span>)).Any(a =&gt; a.EntityKeyProperty == <span style="color:blue;">true</span>));</pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
</p>
<p class="MsoNormal" style="text-indent:7.5pt;line-height:16.5pt;margin:0 0 10pt;"><font size="3"><span style="color:#063e3f;font-family:consolas;">If you have composite primary key then&#160; value of “keyProperties” will be greater than one. For making our&#160; example very simple I assume that here is no composite key here and the datatype of entity key is integer.</span><span style="color:green;font-family:consolas;"> </span></font></p>
</p>
</p>
<pre class="code"><span style="color:#2b91af;">PropertyInfo </span>item = keyProperties[0];
<span style="color:green;">//type generic method
</span><span style="color:#2b91af;">Type </span>typeofClass = <span style="color:blue;">typeof</span>(ClassOfGenericMethod);

<span style="color:green;">//take the specific static method
</span><span style="color:#2b91af;">MethodInfo </span>methodInfo = typeofClass.GetMethod(<span style="color:#a31515;">&quot;GetMaxValue&quot;</span>, <span style="color:#2b91af;">BindingFlags</span>.Public);

<span style="color:green;">// Binding the method info to generic arguments
</span><span style="color:#2b91af;">Type</span>[] genericArguments = <span style="color:blue;">new </span><span style="color:#2b91af;">Type</span>[] { entity.GetType() };
<span style="color:#2b91af;">MethodInfo </span>genericMethodInfo = methodInfo.MakeGenericMethod(genericArguments);
<span style="color:green;">// Invoke the method and passing parameters
// The null parameter is the object to call the method from.if it is static, pass null.
</span><span style="color:blue;">object </span>returnValue = genericMethodInfo.Invoke(<span style="color:blue;">null</span>, <span style="color:blue;">new object</span>[] { context, item.Name });
<span style="color:green;">//set max value
</span>item.SetValue(entity, (<span style="color:blue;">long</span>)result + 1, <span style="color:blue;">null</span>); </pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
</p>
<p class="MsoNormal" style="text-indent:7.5pt;line-height:16.5pt;margin:7.5pt 0;"><font size="3"><span style="color:#063e3f;font-family:consolas;">Here I call the “GetMaxValue” and set its value by incease of one to entity-key Property of target object. Anyways Sorry for my bad english.</span><span style="font-family:consolas;"> </span></font></p>
</p>
</p>
</p>
<p></span></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:64cee379-e067-4ae7-be6e-6685884bbf69" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/Entity+Framework+Generic+Expression+Ado.net" rel="tag">Entity Framework Generic Expression Ado.net</a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=15&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/03/24/generic-method-to-query-in-adonet-entity-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://morshedanwar.wordpress.com/2009/03/24/hello-world/</link>
		<comments>http://morshedanwar.wordpress.com/2009/03/24/hello-world/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 11:10:53 +0000</pubDate>
		<dc:creator>morshedanwar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[This is my first post.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=1&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://morshedanwar.files.wordpress.com/2009/03/test.jpg"><img style="border-right:0;border-top:0;display:inline;border-left:0;border-bottom:0;" title="Test" src="http://morshedanwar.files.wordpress.com/2009/03/test-thumb.jpg?w=244&#038;h=184" border="0" alt="Test" width="244" height="184" /></a> This is my first post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/morshedanwar.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/morshedanwar.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/morshedanwar.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=morshedanwar.wordpress.com&amp;blog=7084277&amp;post=1&amp;subd=morshedanwar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://morshedanwar.wordpress.com/2009/03/24/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f13453e4bbc1ee5536db3c3fa4ea5f51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">morshedanwar</media:title>
		</media:content>

		<media:content url="http://morshedanwar.files.wordpress.com/2009/03/test-thumb.jpg" medium="image">
			<media:title type="html">Test</media:title>
		</media:content>
	</item>
	</channel>
</rss>
