Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

Building WPF Applications with Self-Tracking Entity Generator and Visual Studio 2012 - Project Setup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
17 Mar 2013CPOL8 min read 68.5K   3.5K   44  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator and Visual Studio 2012.
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#

DefineMetadata();

CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);

string inputFile = @"SchoolModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.EscapeNamespace(@"SchoolSample.EntityModel");
string codeForSilverlight = @"None";
string codeForWPF = @"WPF4.5";
string codeForClientQuery = @"true";
string codeForIEditableObject = @"true";

System.Data.Entity.Design.PluralizationServices.PluralizationService pluralizationService = 
System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

if (!code.VerifyCaseInsensitiveTypeUniqueness(code.GetAllGlobalItems(ItemCollection), inputFile))
{
    return string.Empty;
}

WriteHeader(fileManager);
#>
#if (SILVERLIGHT || WPF)
<#
BeginNamespace(namespaceName, code);
if (code.GetItemsToGenerate<EnumType>(ItemCollection).GetEnumerator().MoveNext())
{
    WriteEnumCollection();
}
WriteNotifyValidationResultChanged();
WriteINodeLevelInterface();
WriteClientChangeTracking();
if (codeForClientQuery == "true")
{
    WriteIClientQueryOfT();
    WriteClientQueryOfT();
#>

/// <summary>
/// ClientQuerySet provides properties which we can use
/// as data sources for client queries.
/// </summary>
public static class ClientQuerySet
{
<#
    region.Begin("ClientQuerySet Properties");
    foreach (EntityType entity in code.GetItemsToGenerate<EntityType>(ItemCollection).OrderBy(e => e.Name))
    {
#>

    public static ClientQuery<<#=code.Escape(entity)#>> <#=pluralizationService.Pluralize(code.Escape(entity))#>
    {
        get { return new ClientQuery<<#=code.Escape(entity)#>>(); }
    }
<#
    }
    region.End();
#>
}
<#
    if (codeForSilverlight == @"Silverlight4" || codeForSilverlight == @"Silverlight5")
    {
        WriteClientFilterOfT();
    }
    else if (codeForWPF == @"WPF4" || codeForWPF == @"WPF4.5")
    {
        WriteClientFilterOfTForWPF();
    }
    WriteSortExpression();
    WriteRemoveInvokeVisitor();
    WriteSerializer();
    WriteDeserializer();
    WriteTypeResolver();
}
EndNamespace(namespaceName);
#>
#endif
<#

// Block for Entity Types
foreach (EntityType entity in code.GetItemsToGenerate<EntityType>(ItemCollection).OrderBy(e => e.Name))
{
    if (codeForSilverlight == @"Silverlight4" || codeForSilverlight == @"Silverlight5")
    {
        if (codeForSilverlight == @"Silverlight4")
            fileManager.StartNewFile("SL4." + entity.Name + ".cs");
        else
            fileManager.StartNewFile("SL5." + entity.Name + ".cs");
#>
#if SILVERLIGHT
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.GetTypeName(entity.BaseType))#><#=entity.BaseType == null ? " : " : ", "#>IClientChangeTracking, IChangeTracking, IRevertibleChangeTracking,
                             INotifyDataErrorInfo, INodeLevel<#=codeForIEditableObject == "true" ? ", IEditableObject" : "" #>
{
<#
        region.Begin("IClientChangeTracking Interface");
        WriteAcceptChangesAndRejectChanges();
        if (entity.BaseType == null)
        {
            WriteHasChangesProperty();
        }
#>

    /// <summary>
    /// Accepts changes made to the entity object and all objects of its object graph
    /// </summary>
    public <#=entity.BaseType == null ? "virtual " : "override " #>void AcceptObjectGraphChanges()
    {
        this.AcceptChanges();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        // call AccetChanges() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
            item.AcceptChanges();
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
            <#=code.Escape(navProperty)#>.AcceptChanges();
<#
            }
        }
        if (entity.BaseType != null)
        {
#>
        // call base AcceptObjectGraphChanges() 
        base.AcceptObjectGraphChanges();
<#
        }
#>
    }

    /// <summary>
    /// Rejects changes made to the entity object and all objects of its object graph
    /// </summary>
    public <#=entity.BaseType == null ? "virtual " : "override " #>void RejectObjectGraphChanges()
    {
        this.RejectChanges();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        // call RejectChanges() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
            item.RejectChanges();
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
            <#=code.Escape(navProperty)#>.RejectChanges();
<#
            }
        }
        if (entity.BaseType != null)
        {
#>
        // call base RejectObjectGraphChanges() 
        base.RejectObjectGraphChanges();
<#
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns whether the entity object along with its object graph has any changes
    /// </summary>
    public bool ObjectGraphHasChanges()
    {
        var visitedGraph = new List<object>();
        return ObjectGraphHasChanges(ref visitedGraph);
    }

    internal virtual bool ObjectGraphHasChanges(ref List<object> visitedGraph)
    {
        // if already visited this object, just return false
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return false;

        var hasChanges = HasChanges;
        if (hasChanges) return true;

        // if not, add itself to the visited graph
        visitedGraph.Add(this);
<#
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

        // call ObjectGraphHasChanges() on all Navigation properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            hasChanges = item.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    internal virtual bool ObjectGraphHasChanges(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var hasChanges = HasChanges;
        if (hasChanges) return true;
<#
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

        // if not, find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call ObjectGraphHasChanges() on all Navigation properties of the next level
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value == (currentLevel + 1)))
            {
                hasChanges = item.ObjectGraphHasChanges(nodeLevelDictionary);
                if (hasChanges) return true;
            }
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value == (currentLevel + 1)))
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    internal Dictionary<INodeLevel, int> GetNodeLevelDictionary()
    {
        int currentLevel = 1; // start with level one
        var nodeLevelDictionary = new Dictionary<INodeLevel, int> { { this, currentLevel } };
        bool foundNextLevelNodes;
    
        do
        {
            var nextLevelNodes = new List<INodeLevel>();
            var level = currentLevel;
            // search for the next level nodes using what are currently in the nodeLevelDictionary
            foreach (var n in nodeLevelDictionary.Where(i => i.Value == level))
            {
                nextLevelNodes.AddRange(n.Key.GetNextLevelNodes(nodeLevelDictionary));
            }
            foundNextLevelNodes = nextLevelNodes.Count > 0;
            if (foundNextLevelNodes)
            {
                currentLevel++;
                foreach (var node in nextLevelNodes)
                    nodeLevelDictionary.Add(node, currentLevel);
            }
        } while (foundNextLevelNodes);
    
        return nodeLevelDictionary;
    }

    public virtual List<INodeLevel> GetNextLevelNodes(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var nextLevelNodes = new List<INodeLevel>();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // loop through Navigation properties to add next level nodes
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        nextLevelNodes.AddRange(
            <#=codeForSilverlight == @"Silverlight4" ? "(IEnumerable<INodeLevel>) " : "" #><#=code.Escape(navProperty)#>.Where(
                n => nodeLevelDictionary
                         .All(i => !ReferenceEquals(i.Key, n))));
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null && nodeLevelDictionary.All(i => !ReferenceEquals(i.Key, <#=code.Escape(navProperty)#>)))
            nextLevelNodes.Add(<#=code.Escape(navProperty)#>);
<#
            }
        }
#>

        return nextLevelNodes;
    }
<#
        }
        else
        {
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

    internal override bool ObjectGraphHasChanges(ref List<object> visitedGraph)
    {
        // if already visited this object, just return false
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return false;

        var hasChanges = base.ObjectGraphHasChanges(ref visitedGraph);
        if (hasChanges) return true;

        // if not, add itself to the visited graph
        if (visitedGraph.All(i => !ReferenceEquals(i, this))) visitedGraph.Add(this);

        // call ObjectGraphHasChanges() on all Navigation properties
<#
                foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
                {
                    if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                    {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            hasChanges = item.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                    }
                }
#>
        return false;
    }

    internal override bool ObjectGraphHasChanges(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var hasChanges = base.ObjectGraphHasChanges(nodeLevelDictionary);
        if (hasChanges) return true;

        // if not, find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call ObjectGraphHasChanges() on all Navigation properties of the next level
<#
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value == (currentLevel + 1)))
            {
                hasChanges = item.ObjectGraphHasChanges(nodeLevelDictionary);
                if (hasChanges) return true;
            }
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value == (currentLevel + 1)))
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    public override List<INodeLevel> GetNextLevelNodes(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        // call base GetNextLevelNodes()
        var nextLevelNodes = base.GetNextLevelNodes(nodeLevelDictionary);
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // loop through Navigation properties to add next level nodes
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        nextLevelNodes.AddRange(
            <#=codeForSilverlight == @"Silverlight4" ? "(IEnumerable<INodeLevel>) " : "" #><#=code.Escape(navProperty)#>.Where(
                n => nodeLevelDictionary
                         .All(i => !ReferenceEquals(i.Key, n))));
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null && nodeLevelDictionary.All(i => !ReferenceEquals(i.Key, <#=code.Escape(navProperty)#>)))
            nextLevelNodes.Add(<#=code.Escape(navProperty)#>);
<#
            }
        }
#>

        return nextLevelNodes;
    }
<#
            }
        }

        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns the estimate size of the entity object along with its object graph
    /// </summary>
    public long EstimateObjectGraphSize()
    {
        long size = 0;
        var visitedGraph = new List<object>();
        EstimateObjectGraphSize(ref size, ref visitedGraph);
        return size;
    }
<#
        }
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>void EstimateObjectGraphSize(ref long size, ref List<object> visitedGraph)
    {
        // if already visited this object, just return
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
<#
        if (entity.BaseType != null)
        {
#>

        // call base EstimateObjectGraphSize()
        base.EstimateObjectGraphSize(ref size, ref visitedGraph);
<#
        }
#>

        size += EstimateSize;
        // add itself to the visited graph
        if (visitedGraph.All(i => !ReferenceEquals(i, this))) visitedGraph.Add(this);
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // call EstimateObjectGraphSize() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            item.EstimateObjectGraphSize(ref size, ref visitedGraph);
        }
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            <#=code.Escape(navProperty)#>.EstimateObjectGraphSize(ref size, ref visitedGraph);
        }
<#
            }
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns the estimate size of the optimized entity object graph
    /// with only objects that have changes
    /// </summary>
    public long EstimateObjectGraphChangeSize()
    {
        long size = 0;
        if (!ObjectGraphHasChanges()) return size;
        var nodeLevelDictionary = GetNodeLevelDictionary();
        EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
        return size;
    }
<#
        }
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>void EstimateObjectGraphChangeSize(ref long size, Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
<#
        if (entity.BaseType != null)
        {
#>
        // call base EstimateObjectGraphChangeSize()
        base.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);

<#
        }
#>
        size += EstimateSize;
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call EstimateObjectGraphChangeSize() on all Navigation properties that has change
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to add its size
                    item.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if item has changes, we also need to add its size
                    item.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
                }
            }
        }
<#
            }
            else
            {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
                <#=code.Escape(navProperty)#>.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
        }
<#
            }
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns an optimized entity object graph with only objects that have changes
    /// </summary>
    public IObjectWithChangeTracker GetObjectGraphChanges()
    {
        if (!ObjectGraphHasChanges()) return null;

        var item = this.Clone();
        var nodeLevelDictionary = item.GetNodeLevelDictionary();

        // loop through all navigation properties and trim any unchanged items
        item.TrimUnchangedEntities(nodeLevelDictionary);

        return item;
    }

    internal virtual void TrimUnchangedEntities(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // trim all navigation property items that do not have any change
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>.ToList())
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if n has changes, we also need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else
                {
                    <#=code.Escape(navProperty)#>.Remove(item);
                }
            }
        }
<#
            }
            else
            {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
            {
                <#=code.Escape(navProperty)#>.TrimUnchangedEntities(nodeLevelDictionary);
            }
            else
            {
                <#=code.Escape(navProperty)#> = null;
            }
        }
<#
            }
        }
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
<#
        }
#>
    }
<#
        }
        else
        {
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

    internal override void TrimUnchangedEntities(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        // call base TrimUnchangedEntities() first
        base.TrimUnchangedEntities(nodeLevelDictionary);

        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // trim all navigation property items that do not have any change
<#
                foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
                {
                    if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                    {
#>
        foreach (var item in <#=code.Escape(navProperty)#>.ToList())
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if n has changes, we also need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else
                {
                    <#=code.Escape(navProperty)#>.Remove(item);
                }
            }
        }
<#
                    }
                    else
                    {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
            {
                <#=code.Escape(navProperty)#>.TrimUnchangedEntities(nodeLevelDictionary);
            }
            else
            {
                <#=code.Escape(navProperty)#> = null;
            }
        }
<#
                    }
                }
#>

        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
    }
<#
            }
        }
        region.End();
        region.Begin("IClientChangeTracking Helper Property");
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>long EstimateSize
    {
        get
        {
            long _size = 0;
<#
        if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // estimate size of all Simple Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
        {
            if (edmProperty.TypeUsage.EdmType is PrimitiveType)
            {
                switch (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Binary:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof (Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Boolean:
#>
            _size += sizeof(Boolean);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTime:
#>
            _size += sizeof(ulong);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTimeOffset:
#>
            _size += sizeof(ulong) + sizeof(short);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Decimal:
#>
            _size += sizeof(Decimal);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Double:
#>
            _size += sizeof(Double);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Guid:
#>
            _size += sizeof(int) + sizeof(short) * 2 + sizeof(byte) * 8;    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Single:
#>
            _size += sizeof(Single);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.String:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof(char);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Time:
#>
            _size += sizeof(long);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Geometry:
                    case PrimitiveTypeKind.GeometryPoint:
                    case PrimitiveTypeKind.GeometryLineString:
                    case PrimitiveTypeKind.GeometryPolygon:
                    case PrimitiveTypeKind.GeometryMultiPoint:
                    case PrimitiveTypeKind.GeometryMultiLineString:
                    case PrimitiveTypeKind.GeometryMultiPolygon:
                    case PrimitiveTypeKind.GeometryCollection:
                    case PrimitiveTypeKind.Geography:
                    case PrimitiveTypeKind.GeographyPoint:
                    case PrimitiveTypeKind.GeographyLineString:
                    case PrimitiveTypeKind.GeographyPolygon:
                    case PrimitiveTypeKind.GeographyMultiPoint:
                    case PrimitiveTypeKind.GeographyMultiLineString:
                    case PrimitiveTypeKind.GeographyMultiPolygon:
                    case PrimitiveTypeKind.GeographyCollection:
#>
            if (<#=code.Escape(edmProperty)#> != null)
            {
                using (var stream = new MemoryStream())
                {
                    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    formatter.Serialize(stream, <#=code.Escape(edmProperty)#>);
                    _size += stream.Length;    // <#=code.Escape(edmProperty)#>
                }
            }
<#
                        break;
                }
            }
            else if (edmProperty.TypeUsage.EdmType is EnumType)
            {
                switch (((EnumType)edmProperty.TypeUsage.EdmType).UnderlyingType.PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                }
            }
        }
        if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // get EstimateSize of all Complex Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
        {
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.EstimateSize;    // <#=code.Escape(edmProperty)#>
<#
        }
#>
            return _size;
        }
    }
<#
        region.End();
        region.Begin("IChangeTracking and IRevertibleChangeTracking interfaces");
        WriteIRevertibleChangeTrackingInterface();
        region.End();
        region.Begin("INotifyDataErrorInfo interface");
        WriteINotifyDataErrorInfoInterfaceForEntityType(entity);
        region.End();
        region.Begin("INotifyDataErrorInfo Protected & Private Helper Methods");
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the entity if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (!ValidationErrors.ContainsKey(propertyName))
        {
            errors = new List<ValidationResult>();
            ValidationErrors.Add(propertyName, errors);
        }
        else
        {
            errors = ValidationErrors[propertyName];
        }
        // search for duplicate error message with the same propertyName
        var foundError = errors.FirstOrDefault(n => 
            n.ErrorMessage == validationResult.ErrorMessage);
        if (foundError == null)
        {
            errors.Insert(0, validationResult);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (ValidationErrors.ContainsKey(propertyName))
        {
            errors = ValidationErrors[propertyName];
            // search for the error message that need to be removed
            var foundError = errors.FirstOrDefault(n =>
                n.ErrorMessage == validationResult.ErrorMessage);
            if (foundError != null)
            {
                errors.Remove(foundError);
                if (errors.Count == 0)
                {
                    // This entity no longer exposes errors for this property name.
                    ValidationErrors.Remove(propertyName);
                }
                RaiseErrorsChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            // This entity no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            // This entity no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any entity property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;
<#
        }
#>

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
        {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
        }
        region.End();
        region.Begin("IEditableObject interface");
        if (codeForIEditableObject == "true")
        {
#>

    private Dictionary<string , object> _cache;

    public <#=entity.BaseType == null ? "virtual " : "override " #>void BeginEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base BeginEdit() first
        base.BeginEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Simple Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
#>
        _cache["<#=code.Escape(edmProperty)#>"] = <#=code.Escape(edmProperty)#>;
<#
                }
            }
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call BeginEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).BeginEdit();
<#
            }
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Navigation Properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
#>
        _cache["<#=code.Escape(navProperty)#>"] = <#=code.Escape(navProperty)#>;
<#
            }
            if (entity.BaseType == null)
            {
#>
        // copy ChangeTracker
        _cache["ChangeTracker"] = ChangeTracker.Clone();
<#
            }
#>
    }

    public <#=entity.BaseType == null ? "virtual " : "override " #>void CancelEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base CancelEdit() first
        base.CancelEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
        if (_cache.Count == 0) return;
        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Simple Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
                    if (ef.UnderlyingClrType(edmProperty.TypeUsage.EdmType) == typeof(byte[]) &&
                        entity.NavigationProperties.Where(np=>np.GetDependentProperties().Contains(edmProperty)).Any())
                    {
#>
        if (!EqualityComparer.BinaryEquals(<#=code.Escape(edmProperty)#>, (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"]))
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(edmProperty)#> != (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"])
<#
                    }
#>
            <#=code.Escape(edmProperty)#> = (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"];
        else
            OnPropertyChanged("<#=edmProperty.Name#>");
<#
                }
            }
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call CancelEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).CancelEdit();
<#
            }
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Navigation Properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        <#=code.Escape(navProperty)#> = (TrackableCollection<<#=code.GetTypeName(navProperty.ToEndMember.GetEntityType())#>>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
                else
                {
#>
        <#=code.Escape(navProperty)#> = (<#=code.GetTypeName(navProperty.ToEndMember.GetEntityType())#>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
            }
            if (entity.BaseType == null)
            {
#>
        // copy ChangeTracker
        ChangeTracker = (ObjectChangeTracker)_cache["ChangeTracker"];
<#
            }
#>
        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
        _cache.Clear();
    }

    public <#=entity.BaseType == null ? "virtual " : "override " #>void EndEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base EndEdit() first
        base.EndEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
        _cache.Clear();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call EndEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).EndEdit();
<#
            }
#>
    }
<#
        }
        region.End();
#>
}
<#
        EndNamespace(namespaceName);
#>
#endif
<#
    }
    if (codeForWPF == @"WPF4" || codeForWPF == @"WPF4.5")
    {
        if (codeForWPF == @"WPF4")
            fileManager.StartNewFile("WPF4." + entity.Name + ".cs");
        else
            fileManager.StartNewFile("WPF4.5." + entity.Name + ".cs");
#>
#if WPF
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.GetTypeName(entity.BaseType))#><#=entity.BaseType == null ? " : " : ", "#>IClientChangeTracking, IChangeTracking, IRevertibleChangeTracking,
                             <#=codeForWPF == @"WPF4" ? "IDataErrorInfo" : "INotifyDataErrorInfo" #>, INodeLevel<#=codeForIEditableObject == "true" ? ", IEditableObject" : "" #>
{
<#
        region.Begin("IClientChangeTracking Interface");
        WriteAcceptChangesAndRejectChanges();
        if (entity.BaseType == null)
        {
            WriteHasChangesProperty();
        }
#>

    /// <summary>
    /// Accepts changes made to the entity object and all objects of its object graph
    /// </summary>
    public <#=entity.BaseType == null ? "virtual " : "override " #>void AcceptObjectGraphChanges()
    {
        this.AcceptChanges();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        // call AccetChanges() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
            item.AcceptChanges();
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
            <#=code.Escape(navProperty)#>.AcceptChanges();
<#
            }
        }
        if (entity.BaseType != null)
        {
#>
        // call base AcceptObjectGraphChanges() 
        base.AcceptObjectGraphChanges();
<#
        }
#>
    }

    /// <summary>
    /// Rejects changes made to the entity object and all objects of its object graph
    /// </summary>
    public <#=entity.BaseType == null ? "virtual " : "override " #>void RejectObjectGraphChanges()
    {
        this.RejectChanges();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        // call RejectChanges() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
            item.RejectChanges();
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
            <#=code.Escape(navProperty)#>.RejectChanges();
<#
            }
        }
        if (entity.BaseType != null)
        {
#>
        // call base RejectObjectGraphChanges() 
        base.RejectObjectGraphChanges();
<#
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns whether the entity object along with its object graph has any changes
    /// </summary>
    public bool ObjectGraphHasChanges()
    {
        var visitedGraph = new List<object>();
        return ObjectGraphHasChanges(ref visitedGraph);
    }

    internal virtual bool ObjectGraphHasChanges(ref List<object> visitedGraph)
    {
        // if already visited this object, just return false
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return false;

        var hasChanges = HasChanges;
        if (hasChanges) return true;

        // if not, add itself to the visited graph
        visitedGraph.Add(this);
<#
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

        // call ObjectGraphHasChanges() on all Navigation properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            hasChanges = item.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    internal virtual bool ObjectGraphHasChanges(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var hasChanges = HasChanges;
        if (hasChanges) return true;
<#
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

        // if not, find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call ObjectGraphHasChanges() on all Navigation properties of the next level
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value == (currentLevel + 1)))
            {
                hasChanges = item.ObjectGraphHasChanges(nodeLevelDictionary);
                if (hasChanges) return true;
            }
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value == (currentLevel + 1)))
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    internal Dictionary<INodeLevel, int> GetNodeLevelDictionary()
    {
        int currentLevel = 1; // start with level one
        var nodeLevelDictionary = new Dictionary<INodeLevel, int> { { this, currentLevel } };
        bool foundNextLevelNodes;
    
        do
        {
            var nextLevelNodes = new List<INodeLevel>();
            var level = currentLevel;
            // search for the next level nodes using what are currently in the nodeLevelDictionary
            foreach (var n in nodeLevelDictionary.Where(i => i.Value == level))
            {
                nextLevelNodes.AddRange(n.Key.GetNextLevelNodes(nodeLevelDictionary));
            }
            foundNextLevelNodes = nextLevelNodes.Count > 0;
            if (foundNextLevelNodes)
            {
                currentLevel++;
                foreach (var node in nextLevelNodes)
                    nodeLevelDictionary.Add(node, currentLevel);
            }
        } while (foundNextLevelNodes);
    
        return nodeLevelDictionary;
    }

    public virtual List<INodeLevel> GetNextLevelNodes(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var nextLevelNodes = new List<INodeLevel>();
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // loop through Navigation properties to add next level nodes
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        nextLevelNodes.AddRange(
            <#=code.Escape(navProperty)#>.Where(
                n => nodeLevelDictionary
                         .All(i => !ReferenceEquals(i.Key, n))));
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null && nodeLevelDictionary.All(i => !ReferenceEquals(i.Key, <#=code.Escape(navProperty)#>)))
            nextLevelNodes.Add(<#=code.Escape(navProperty)#>);
<#
            }
        }
#>

        return nextLevelNodes;
    }
<#
        }
        else
        {
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

    internal override bool ObjectGraphHasChanges(ref List<object> visitedGraph)
    {
        // if already visited this object, just return false
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return false;

        var hasChanges = base.ObjectGraphHasChanges(ref visitedGraph);
        if (hasChanges) return true;

        // if not, add itself to the visited graph
        if (visitedGraph.All(i => !ReferenceEquals(i, this))) visitedGraph.Add(this);

        // call ObjectGraphHasChanges() on all Navigation properties
<#
                foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
                {
                    if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                    {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            hasChanges = item.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(ref visitedGraph);
            if (hasChanges) return true;
        }
<#
                    }
                }
#>
        return false;
    }

    internal override bool ObjectGraphHasChanges(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        var hasChanges = base.ObjectGraphHasChanges(nodeLevelDictionary);
        if (hasChanges) return true;

        // if not, find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call ObjectGraphHasChanges() on all Navigation properties of the next level
<#
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value == (currentLevel + 1)))
            {
                hasChanges = item.ObjectGraphHasChanges(nodeLevelDictionary);
                if (hasChanges) return true;
            }
        }
<#
                }
                else
                {
#>
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value == (currentLevel + 1)))
        {
            hasChanges = <#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary);
            if (hasChanges) return true;
        }
<#
                }
            }
#>
        return false;
    }

    public override List<INodeLevel> GetNextLevelNodes(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        // call base GetNextLevelNodes()
        var nextLevelNodes = base.GetNextLevelNodes(nodeLevelDictionary);
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // loop through Navigation properties to add next level nodes
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        nextLevelNodes.AddRange(
            <#=code.Escape(navProperty)#>.Where(
                n => nodeLevelDictionary
                         .All(i => !ReferenceEquals(i.Key, n))));
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null && nodeLevelDictionary.All(i => !ReferenceEquals(i.Key, <#=code.Escape(navProperty)#>)))
            nextLevelNodes.Add(<#=code.Escape(navProperty)#>);
<#
            }
        }
#>

        return nextLevelNodes;
    }
<#
            }
        }

        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns the estimate size of the entity object along with its object graph
    /// </summary>
    public long EstimateObjectGraphSize()
    {
        long size = 0;
        var visitedGraph = new List<object>();
        EstimateObjectGraphSize(ref size, ref visitedGraph);
        return size;
    }
<#
        }
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>void EstimateObjectGraphSize(ref long size, ref List<object> visitedGraph)
    {
        // if already visited this object, just return
        if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
<#
        if (entity.BaseType != null)
        {
#>

        // call base EstimateObjectGraphSize()
        base.EstimateObjectGraphSize(ref size, ref visitedGraph);
<#
        }
#>

        size += EstimateSize;
        // add itself to the visited graph
        if (visitedGraph.All(i => !ReferenceEquals(i, this))) visitedGraph.Add(this);
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // call EstimateObjectGraphSize() on all Navigation properties
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            item.EstimateObjectGraphSize(ref size, ref visitedGraph);
        }
<#
            }
            else
            {
#>
        if (<#=code.Escape(navProperty)#> != null)
        {
            <#=code.Escape(navProperty)#>.EstimateObjectGraphSize(ref size, ref visitedGraph);
        }
<#
            }
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns the estimate size of the optimized entity object graph
    /// with only objects that have changes
    /// </summary>
    public long EstimateObjectGraphChangeSize()
    {
        long size = 0;
        if (!ObjectGraphHasChanges()) return size;
        var nodeLevelDictionary = GetNodeLevelDictionary();
        EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
        return size;
    }
<#
        }
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>void EstimateObjectGraphChangeSize(ref long size, Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
<#
        if (entity.BaseType != null)
        {
#>
        // call base EstimateObjectGraphChangeSize()
        base.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);

<#
        }
#>
        size += EstimateSize;
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // call EstimateObjectGraphChangeSize() on all Navigation properties that has change
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>)
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to add its size
                    item.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if item has changes, we also need to add its size
                    item.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
                }
            }
        }
<#
            }
            else
            {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
                <#=code.Escape(navProperty)#>.EstimateObjectGraphChangeSize(ref size, nodeLevelDictionary);
        }
<#
            }
        }
#>
    }
<#
        if (entity.BaseType == null)
        {
#>

    /// <summary>
    /// Returns an optimized entity object graph with only objects that have changes
    /// </summary>
    public IObjectWithChangeTracker GetObjectGraphChanges()
    {
        if (!ObjectGraphHasChanges()) return null;

        var item = this.Clone();
        var nodeLevelDictionary = item.GetNodeLevelDictionary();

        // loop through all navigation properties and trim any unchanged items
        item.TrimUnchangedEntities(nodeLevelDictionary);

        return item;
    }

    internal virtual void TrimUnchangedEntities(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
<#
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>
        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // trim all navigation property items that do not have any change
<#
        }
        foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
        {
            if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
        foreach (var item in <#=code.Escape(navProperty)#>.ToList())
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if n has changes, we also need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else
                {
                    <#=code.Escape(navProperty)#>.Remove(item);
                }
            }
        }
<#
            }
            else
            {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
            {
                <#=code.Escape(navProperty)#>.TrimUnchangedEntities(nodeLevelDictionary);
            }
            else
            {
                <#=code.Escape(navProperty)#> = null;
            }
        }
<#
            }
        }
        if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
        {
#>

        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
<#
        }
#>
    }
<#
        }
        else
        {
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>

    internal override void TrimUnchangedEntities(Dictionary<INodeLevel, int> nodeLevelDictionary)
    {
        // call base TrimUnchangedEntities() first
        base.TrimUnchangedEntities(nodeLevelDictionary);

        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();

        // find out the current node level
        var currentLevel = nodeLevelDictionary.Single(n => ReferenceEquals(n.Key, this)).Value;

        // trim all navigation property items that do not have any change
<#
                foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
                {
                    if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                    {
#>
        foreach (var item in <#=code.Escape(navProperty)#>.ToList())
        {
            if (nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, item) && n.Value != (currentLevel - 1)))
            {
                if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("<#=code.Escape(navProperty)#>")
                    && ChangeTracker.ObjectsAddedToCollectionProperties["<#=code.Escape(navProperty)#>"].Contains(item))
                {
                    // if item exists in ObjectsAddedToCollectionProperties, we need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else if (item.ObjectGraphHasChanges(nodeLevelDictionary))
                {
                    // if n has changes, we also need to keep it
                    item.TrimUnchangedEntities(nodeLevelDictionary);
                }
                else
                {
                    <#=code.Escape(navProperty)#>.Remove(item);
                }
            }
        }
<#
                    }
                    else
                    {
#>
        // if <#=code.Escape(navProperty)#> has already been visited, we need to skip it
        if (<#=code.Escape(navProperty)#> != null &&
            nodeLevelDictionary.Any(n => ReferenceEquals(n.Key, <#=code.Escape(navProperty)#>) && n.Value != (currentLevel - 1)))
        {
            if (<#=code.Escape(navProperty)#>.ObjectGraphHasChanges(nodeLevelDictionary))
            {
                <#=code.Escape(navProperty)#>.TrimUnchangedEntities(nodeLevelDictionary);
            }
            else
            {
                <#=code.Escape(navProperty)#> = null;
            }
        }
<#
                    }
                }
#>

        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
    }
<#
            }
        }
        region.End();
        region.Begin("IClientChangeTracking Helper Property");
#>

    internal <#=entity.BaseType == null ? "virtual " : "override " #>long EstimateSize
    {
        get
        {
            long _size = 0;
<#
        if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // estimate size of all Simple Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
        {
            if (edmProperty.TypeUsage.EdmType is PrimitiveType)
            {
                switch (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Binary:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof (Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Boolean:
#>
            _size += sizeof(Boolean);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTime:
#>
            _size += sizeof(ulong);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTimeOffset:
#>
            _size += sizeof(ulong) + sizeof(short);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Decimal:
#>
            _size += sizeof(Decimal);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Double:
#>
            _size += sizeof(Double);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Guid:
#>
            _size += sizeof(int) + sizeof(short) * 2 + sizeof(byte) * 8;    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Single:
#>
            _size += sizeof(Single);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.String:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof(char);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Time:
#>
            _size += sizeof(long);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Geometry:
                    case PrimitiveTypeKind.GeometryPoint:
                    case PrimitiveTypeKind.GeometryLineString:
                    case PrimitiveTypeKind.GeometryPolygon:
                    case PrimitiveTypeKind.GeometryMultiPoint:
                    case PrimitiveTypeKind.GeometryMultiLineString:
                    case PrimitiveTypeKind.GeometryMultiPolygon:
                    case PrimitiveTypeKind.GeometryCollection:
                    case PrimitiveTypeKind.Geography:
                    case PrimitiveTypeKind.GeographyPoint:
                    case PrimitiveTypeKind.GeographyLineString:
                    case PrimitiveTypeKind.GeographyPolygon:
                    case PrimitiveTypeKind.GeographyMultiPoint:
                    case PrimitiveTypeKind.GeographyMultiLineString:
                    case PrimitiveTypeKind.GeographyMultiPolygon:
                    case PrimitiveTypeKind.GeographyCollection:
#>
            if (<#=code.Escape(edmProperty)#> != null)
            {
                using (var stream = new MemoryStream())
                {
                    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    formatter.Serialize(stream, <#=code.Escape(edmProperty)#>);
                    _size += stream.Length;    // <#=code.Escape(edmProperty)#>
                }
            }
<#
                        break;
                }
            }
            else if (edmProperty.TypeUsage.EdmType is EnumType)
            {
                switch (((EnumType)edmProperty.TypeUsage.EdmType).UnderlyingType.PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                }
            }
        }
        if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // get EstimateSize of all Complex Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
        {
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.EstimateSize;    // <#=code.Escape(edmProperty)#>
<#
        }
#>
            return _size;
        }
    }
<#
        region.End();
        region.Begin("IChangeTracking and IRevertibleChangeTracking interfaces");
        WriteIRevertibleChangeTrackingInterface();
        region.End();
        if (codeForWPF == @"WPF4")
        {
            region.Begin("IDataErrorInfo interface");
            WriteIDataErrorInfoInterfaceForEntityType(entity);
            region.End();
            region.Begin("IDataErrorInfo Protected & Private Helper Methods");
            if (entity.BaseType == null)
            {
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the entity if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (!ValidationErrors.ContainsKey(propertyName))
        {
            ValidationErrors.Add(propertyName, validationResult);
            OnPropertyChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            if (string.Equals(ValidationErrors[propertyName].ErrorMessage, validationResult.ErrorMessage, StringComparison.CurrentCulture))
            {
                // This entity no longer exposes error for this property name.
                ValidationErrors.Remove(propertyName);
                OnPropertyChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            // This entity no longer exposes error for this property name.
            ValidationErrors.Remove(propertyName);
            OnPropertyChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            // This entity no longer exposes error for this property name.
            ValidationErrors.Remove(propertyName);
            OnPropertyChanged(propertyName);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any entity property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;
<#
            }
#>

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
            }
            region.End();
        }
        else
        {
            region.Begin("INotifyDataErrorInfo interface");
            WriteINotifyDataErrorInfoInterfaceForEntityType(entity);
            region.End();
            region.Begin("INotifyDataErrorInfo Protected & Private Helper Methods");
            if (entity.BaseType == null)
            {
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the entity if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (!ValidationErrors.ContainsKey(propertyName))
        {
            errors = new List<ValidationResult>();
            ValidationErrors.Add(propertyName, errors);
        }
        else
        {
            errors = ValidationErrors[propertyName];
        }
        // search for duplicate error message with the same propertyName
        var foundError = errors.FirstOrDefault(n => 
            n.ErrorMessage == validationResult.ErrorMessage);
        if (foundError == null)
        {
            errors.Insert(0, validationResult);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (ValidationErrors.ContainsKey(propertyName))
        {
            errors = ValidationErrors[propertyName];
            // search for the error message that need to be removed
            var foundError = errors.FirstOrDefault(n =>
                n.ErrorMessage == validationResult.ErrorMessage);
            if (foundError != null)
            {
                errors.Remove(foundError);
                if (errors.Count == 0)
                {
                    // This entity no longer exposes errors for this property name.
                    ValidationErrors.Remove(propertyName);
                }
                RaiseErrorsChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            // This entity no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            // This entity no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any entity property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;
<#
            }
#>

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
            }
            region.End();
        }
        region.Begin("IEditableObject interface");
        if (codeForIEditableObject == "true")
        {
#>

    private Dictionary<string , object> _cache;

    public <#=entity.BaseType == null ? "virtual " : "override " #>void BeginEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base BeginEdit() first
        base.BeginEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Simple Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
#>
        _cache["<#=code.Escape(edmProperty)#>"] = <#=code.Escape(edmProperty)#>;
<#
                }
            }
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call BeginEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).BeginEdit();
<#
            }
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Navigation Properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
#>
        _cache["<#=code.Escape(navProperty)#>"] = <#=code.Escape(navProperty)#>;
<#
            }
            if (entity.BaseType == null)
            {
#>
        // copy ChangeTracker
        _cache["ChangeTracker"] = ChangeTracker.Clone();
<#
            }
#>
    }

    public <#=entity.BaseType == null ? "virtual " : "override " #>void CancelEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base CancelEdit() first
        base.CancelEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
        if (_cache.Count == 0) return;
        bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
        this.StopTracking();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Simple Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
                    if (ef.UnderlyingClrType(edmProperty.TypeUsage.EdmType) == typeof(byte[]) &&
                        entity.NavigationProperties.Where(np=>np.GetDependentProperties().Contains(edmProperty)).Any())
                    {
#>
        if (!EqualityComparer.BinaryEquals(<#=code.Escape(edmProperty)#>, (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"]))
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(edmProperty)#> != (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"])
<#
                    }
#>
            <#=code.Escape(edmProperty)#> = (<#=code.GetTypeName(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"];
        else
            OnPropertyChanged("<#=edmProperty.Name#>");
<#
                }
            }
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call CancelEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).CancelEdit();
<#
            }
            if (entity.NavigationProperties.Where(np => np.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Navigation Properties
<#
            }
            foreach (NavigationProperty navProperty in entity.NavigationProperties.Where(np => np.DeclaringType == entity))
            {
                if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
                {
#>
        <#=code.Escape(navProperty)#> = (TrackableCollection<<#=code.GetTypeName(navProperty.ToEndMember.GetEntityType())#>>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
                else
                {
#>
        <#=code.Escape(navProperty)#> = (<#=code.GetTypeName(navProperty.ToEndMember.GetEntityType())#>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
            }
            if (entity.BaseType == null)
            {
#>
        // copy ChangeTracker
        ChangeTracker = (ObjectChangeTracker)_cache["ChangeTracker"];
<#
            }
#>
        ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
        _cache.Clear();
    }

    public <#=entity.BaseType == null ? "virtual " : "override " #>void EndEdit()
    {
<#
            if (entity.BaseType != null)
            {
#>
        // call base EndEdit() first
        base.EndEdit();
<#
            }
#>
        if (_cache == null) _cache = new Dictionary<string, object>();
        _cache.Clear();
<#
            if (entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // call EndEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).EndEdit();
<#
            }
#>
    }
<#
        }
        region.End();
#>
}
<#
        EndNamespace(namespaceName);
#>
#endif
<#
    }
}  // End of Block for Entity Types

// Block for Complex Types
foreach (ComplexType complex in code.GetItemsToGenerate<ComplexType>(ItemCollection).OrderBy(e => e.Name))
{
    if (codeForSilverlight == @"Silverlight4" || codeForSilverlight == @"Silverlight5")
    {
        if (codeForSilverlight == @"Silverlight4")
            fileManager.StartNewFile("SL4." + complex.Name + ".cs");
        else
            fileManager.StartNewFile("SL5." + complex.Name + ".cs");
#>
#if SILVERLIGHT
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#><#=code.StringBefore(" : ", code.GetTypeName(complex.BaseType))#><#=complex.BaseType == null ? " : " : ", "#>INotifyDataErrorInfo, INotifyValidationResultChanged<#=codeForIEditableObject == "true" ? ", IEditableObject" : "" #>
{
<#
        region.Begin("INotifyDataErrorInfo interface");
        WriteINotifyDataErrorInfoInterfaceForComplexType();
        region.End();
        region.Begin("INotifyDataErrorInfo Protected & Private Helper Methods");
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the object if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (!ValidationErrors.ContainsKey(propertyName))
        {
            errors = new List<ValidationResult>();
            ValidationErrors.Add(propertyName, errors);
        }
        else
        {
            errors = ValidationErrors[propertyName];
        }
        // search for duplicate error message with the same propertyName
        var foundError = errors.FirstOrDefault(n => 
            n.ErrorMessage == validationResult.ErrorMessage);
        if (foundError == null)
        {
            errors.Insert(0, validationResult);
            RaiseErrorsChanged(propertyName);
            List<ValidationResult> addedItems = new List<ValidationResult> {validationResult};
            OnValidationResultChanged(propertyName, null, addedItems);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (ValidationErrors.ContainsKey(propertyName))
        {
            errors = ValidationErrors[propertyName];
            // search for the error message that need to be removed
            var foundError = errors.FirstOrDefault(n =>
                n.ErrorMessage == validationResult.ErrorMessage);
            if (foundError != null)
            {
                errors.Remove(foundError);
                if (errors.Count == 0)
                {
                    // This entity no longer exposes errors for this property name.
                    ValidationErrors.Remove(propertyName);
                }
                RaiseErrorsChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            List<ValidationResult> removedItems = new List<ValidationResult>(ValidationErrors[propertyName]);
            // This object no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        List<ValidationResult> removedItems;

        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            removedItems = new List<ValidationResult>(ValidationErrors[propertyName]);
            // This object no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
        {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
        }
        region.End();
        region.Begin("INotifyValidationResultChanged interface");
        WriteINotifyValidationResultChangedInterface();
        region.End();
        region.Begin("IClientChangeTracking Helper Property");
#>

    internal long EstimateSize
    {
        get
        {
            long _size = 0;
<#
        if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // estimate size of all Simple Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
        {
            if (edmProperty.TypeUsage.EdmType is PrimitiveType)
            {
                switch (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Binary:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof (Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Boolean:
#>
            _size += sizeof(Boolean);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTime:
#>
            _size += sizeof(ulong);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTimeOffset:
#>
            _size += sizeof(ulong) + sizeof(short);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Decimal:
#>
            _size += sizeof(Decimal);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Double:
#>
            _size += sizeof(Double);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Guid:
#>
            _size += sizeof(int) + sizeof(short) * 2 + sizeof(byte) * 8;    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Single:
#>
            _size += sizeof(Single);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.String:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof(char);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Time:
#>
            _size += sizeof(long);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Geometry:
                    case PrimitiveTypeKind.GeometryPoint:
                    case PrimitiveTypeKind.GeometryLineString:
                    case PrimitiveTypeKind.GeometryPolygon:
                    case PrimitiveTypeKind.GeometryMultiPoint:
                    case PrimitiveTypeKind.GeometryMultiLineString:
                    case PrimitiveTypeKind.GeometryMultiPolygon:
                    case PrimitiveTypeKind.GeometryCollection:
                    case PrimitiveTypeKind.Geography:
                    case PrimitiveTypeKind.GeographyPoint:
                    case PrimitiveTypeKind.GeographyLineString:
                    case PrimitiveTypeKind.GeographyPolygon:
                    case PrimitiveTypeKind.GeographyMultiPoint:
                    case PrimitiveTypeKind.GeographyMultiLineString:
                    case PrimitiveTypeKind.GeographyMultiPolygon:
                    case PrimitiveTypeKind.GeographyCollection:
#>
            if (<#=code.Escape(edmProperty)#> != null)
            {
                using (var stream = new MemoryStream())
                {
                    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    formatter.Serialize(stream, <#=code.Escape(edmProperty)#>);
                    _size += stream.Length;    // <#=code.Escape(edmProperty)#>
                }
            }
<#
                        break;
                }
            }
            else if (edmProperty.TypeUsage.EdmType is EnumType)
            {
                switch (((EnumType)edmProperty.TypeUsage.EdmType).UnderlyingType.PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                }
            }
        }
        if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // get EstimateSize of all Complex Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
        {
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.EstimateSize;    // <#=code.Escape(edmProperty)#>
<#
        }
#>
            return _size;
        }
    }
<#
        region.End();
        region.Begin("IEditableObject interface");
        if (codeForIEditableObject == "true")
        {
#>

    private <#=code.Escape(complex)#> _cache;

    void IEditableObject.BeginEdit()
    {
        _cache = new <#=code.Escape(complex)#>
        {
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
            {
#>
            // copy all Simple Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
            {
#>
            <#=code.FieldName(edmProperty)#> = <#=code.Escape(edmProperty)#>,
<#
            }
#>
        };
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call BeginEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).BeginEdit();
<#
            }
#>
    }

    void IEditableObject.CancelEdit()
    {
        if (_cache == null) return;
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // copy all Simple Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
            {
#>
        if (<#=code.Escape(edmProperty)#> != _cache.<#=code.FieldName(edmProperty)#>)
            <#=code.Escape(edmProperty)#> = _cache.<#=code.FieldName(edmProperty)#>;
        else
            OnPropertyChanged("<#=edmProperty.Name#>");
<#
            }
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call CancelEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).CancelEdit();
<#
            }
#>
        _cache = null;
    }

    void IEditableObject.EndEdit()
    {
        _cache = null;
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call EndEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).EndEdit();
<#
            }
#>
    }
<#
        }
        region.End();
#>
}
<#
        EndNamespace(namespaceName);
#>
#endif
<#
    }
    if (codeForWPF == @"WPF4" || codeForWPF == @"WPF4.5")
    {
        if (codeForWPF == @"WPF4")
            fileManager.StartNewFile("WPF4." + complex.Name + ".cs");
        else
            fileManager.StartNewFile("WPF4.5." + complex.Name + ".cs");
#>
#if WPF
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#><#=code.StringBefore(" : ", code.GetTypeName(complex.BaseType))#><#=complex.BaseType == null ? " : " : ", "#><#=codeForWPF == @"WPF4" ? "IDataErrorInfo" : "INotifyDataErrorInfo" #>, INotifyValidationResultChanged<#=codeForIEditableObject == "true" ? ", IEditableObject" : "" #>
{
<#
        if (codeForWPF == @"WPF4")
        {
            region.Begin("IDataErrorInfo interface");
            WriteIDataErrorInfoInterfaceForComplexType();
            region.End();
            region.Begin("IDataErrorInfo Protected & Private Helper Methods");
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the object if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (!ValidationErrors.ContainsKey(propertyName))
        {
            ValidationErrors.Add(propertyName, validationResult);
            OnPropertyChanged(propertyName);
            List<ValidationResult> addedItems = new List<ValidationResult> {validationResult};
            OnValidationResultChanged(propertyName, null, addedItems);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            if (string.Equals(ValidationErrors[propertyName].ErrorMessage, validationResult.ErrorMessage, StringComparison.CurrentCulture))
            {
                // This entity no longer exposes error for this property name.
                ValidationErrors.Remove(propertyName);
                OnPropertyChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            List<ValidationResult> removedItems = new List<ValidationResult> {ValidationErrors[propertyName]};
            // This entity no longer exposes error for this property name.
            ValidationErrors.Remove(propertyName);
            OnPropertyChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            List<ValidationResult> removedItems = new List<ValidationResult> { ValidationErrors[propertyName] };
            // This entity no longer exposes error for this property name.
            ValidationErrors.Remove(propertyName);
            OnPropertyChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
            }
            region.End();
        }
        else
        {
            region.Begin("INotifyDataErrorInfo interface");
            WriteINotifyDataErrorInfoInterfaceForComplexType();
            region.End();
            region.Begin("INotifyDataErrorInfo Protected & Private Helper Methods");
#>

    /// <summary>
    /// Declares a new error for the property name provided, or the object if
    /// propertyName is String.Empty/null.
    /// </summary>
    protected void AddError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (!ValidationErrors.ContainsKey(propertyName))
        {
            errors = new List<ValidationResult>();
            ValidationErrors.Add(propertyName, errors);
        }
        else
        {
            errors = ValidationErrors[propertyName];
        }
        // search for duplicate error message with the same propertyName
        var foundError = errors.FirstOrDefault(n => 
            n.ErrorMessage == validationResult.ErrorMessage);
        if (foundError == null)
        {
            errors.Insert(0, validationResult);
            RaiseErrorsChanged(propertyName);
            List<ValidationResult> addedItems = new List<ValidationResult> {validationResult};
            OnValidationResultChanged(propertyName, null, addedItems);
        }
    }

    /// <summary>
    /// Removes one specific error for the provided property name.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="validationResult"></param>
    protected void RemoveError(string propertyName, ValidationResult validationResult)
    {
        if (validationResult == null)
        {
            throw new ArgumentNullException("validationResult");
        }

        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        List<ValidationResult> errors;
        if (ValidationErrors.ContainsKey(propertyName))
        {
            errors = ValidationErrors[propertyName];
            // search for the error message that need to be removed
            var foundError = errors.FirstOrDefault(n =>
                n.ErrorMessage == validationResult.ErrorMessage);
            if (foundError != null)
            {
                errors.Remove(foundError);
                if (errors.Count == 0)
                {
                    // This entity no longer exposes errors for this property name.
                    ValidationErrors.Remove(propertyName);
                }
                RaiseErrorsChanged(propertyName);
            }
        }
    }

    /// <summary>
    /// Removes the known errors for the provided property name.
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    protected void ClearErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            List<ValidationResult> removedItems = new List<ValidationResult>(ValidationErrors[propertyName]);
            // This object no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Removes the known errors for all property names.
    /// </summary>
    protected void ClearErrors()
    {
        List<ValidationResult> removedItems;

        foreach (string propertyName in ValidationErrors.Keys.ToList())
        {
            removedItems = new List<ValidationResult>(ValidationErrors[propertyName]);
            // This object no longer exposes errors for this property name.
            ValidationErrors.Remove(propertyName);
            RaiseErrorsChanged(propertyName);
            OnValidationResultChanged(propertyName, removedItems, null);
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to suspend validation
    /// whenever any entity property changes.
    /// </summary>
    public Boolean SuspendValidation;

    /// <summary>
    /// Gets a value indicating whether or not top-level validation rules
    /// must be applied whenever any property changes.
    /// </summary>
    protected static Boolean ValidateEntityOnPropertyChanged;

    /// <summary>
    /// Removes any known errors for the provided property name
    /// by calling ClearErrors()
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    partial void PropertySetterEntry(string propertyName)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            ClearErrors();
        }
        else
        {
            ClearErrors(propertyName);
        }
    }

    /// <summary>
    /// Validates for any known errors for the provided property name
    /// </summary>
    /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
    /// <param name="propertyValue">Property value</param>
    partial void PropertySetterExit(string propertyName, object propertyValue)
    {
        if (IsDeserializing || SuspendValidation)
        {
            return;
        }

        if (ValidateEntityOnPropertyChanged)
        {
            Validate(string.Empty, this);
        }
        else
        {
            Validate(propertyName, propertyValue);
        }
    }
<#
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>

    // Synchronize validation error changes from Complex Type property
    private void Handle<#=edmProperty.Name#>ValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
    {
        string propertyName;
        if (string.IsNullOrEmpty(e.PropertyName))
            propertyName = "<#=edmProperty.Name#>";
        else
            propertyName = "<#=edmProperty.Name#>." + e.PropertyName;

        foreach (ValidationResult item in e.AddedItems)
        {
            AddError(propertyName, item);
        }
        foreach (ValidationResult item in e.RemovedItems)
        {
            RemoveError(propertyName, item);
        }
    }
<#
            }
            region.End();
        }
        region.Begin("INotifyValidationResultChanged interface");
        WriteINotifyValidationResultChangedInterface();
        region.End();
        region.Begin("IClientChangeTracking Helper Property");
#>

    internal long EstimateSize
    {
        get
        {
            long _size = 0;
<#
        if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // estimate size of all Simple Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
        {
            if (edmProperty.TypeUsage.EdmType is PrimitiveType)
            {
                switch (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Binary:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof (Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Boolean:
#>
            _size += sizeof(Boolean);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTime:
#>
            _size += sizeof(ulong);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.DateTimeOffset:
#>
            _size += sizeof(ulong) + sizeof(short);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Decimal:
#>
            _size += sizeof(Decimal);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Double:
#>
            _size += sizeof(Double);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Guid:
#>
            _size += sizeof(int) + sizeof(short) * 2 + sizeof(byte) * 8;    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Single:
#>
            _size += sizeof(Single);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.String:
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.Length * sizeof(char);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Time:
#>
            _size += sizeof(long);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Geometry:
                    case PrimitiveTypeKind.GeometryPoint:
                    case PrimitiveTypeKind.GeometryLineString:
                    case PrimitiveTypeKind.GeometryPolygon:
                    case PrimitiveTypeKind.GeometryMultiPoint:
                    case PrimitiveTypeKind.GeometryMultiLineString:
                    case PrimitiveTypeKind.GeometryMultiPolygon:
                    case PrimitiveTypeKind.GeometryCollection:
                    case PrimitiveTypeKind.Geography:
                    case PrimitiveTypeKind.GeographyPoint:
                    case PrimitiveTypeKind.GeographyLineString:
                    case PrimitiveTypeKind.GeographyPolygon:
                    case PrimitiveTypeKind.GeographyMultiPoint:
                    case PrimitiveTypeKind.GeographyMultiLineString:
                    case PrimitiveTypeKind.GeographyMultiPolygon:
                    case PrimitiveTypeKind.GeographyCollection:
#>
            if (<#=code.Escape(edmProperty)#> != null)
            {
                using (var stream = new MemoryStream())
                {
                    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    formatter.Serialize(stream, <#=code.Escape(edmProperty)#>);
                    _size += stream.Length;    // <#=code.Escape(edmProperty)#>
                }
            }
<#
                        break;
                }
            }
            else if (edmProperty.TypeUsage.EdmType is EnumType)
            {
                switch (((EnumType)edmProperty.TypeUsage.EdmType).UnderlyingType.PrimitiveTypeKind)
                {
                    case PrimitiveTypeKind.Int16:
#>
            _size += sizeof(Int16);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int32:
#>
            _size += sizeof(Int32);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Int64:
#>
            _size += sizeof(Int64);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.Byte:
#>
            _size += sizeof(Byte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                    case PrimitiveTypeKind.SByte:
#>
            _size += sizeof(SByte);    // <#=code.Escape(edmProperty)#>
<#
                        break;
                }
            }
        }
        if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // get EstimateSize of all Complex Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
        {
#>
            if (<#=code.Escape(edmProperty)#> != null)
                _size += <#=code.Escape(edmProperty)#>.EstimateSize;    // <#=code.Escape(edmProperty)#>
<#
        }
#>
            return _size;
        }
    }
<#
        region.End();
        region.Begin("IEditableObject interface");
        if (codeForIEditableObject == "true")
        {
#>

    private <#=code.Escape(complex)#> _cache;

    void IEditableObject.BeginEdit()
    {
        _cache = new <#=code.Escape(complex)#>
        {
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
            {
#>
            // copy all Simple Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
            {
#>
            <#=code.FieldName(edmProperty)#> = <#=code.Escape(edmProperty)#>,
<#
            }
#>
        };
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call BeginEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).BeginEdit();
<#
            }
#>
    }

    void IEditableObject.CancelEdit()
    {
        if (_cache == null) return;
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // copy all Simple Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == complex))
            {
#>
        if (<#=code.Escape(edmProperty)#> != _cache.<#=code.FieldName(edmProperty)#>)
            <#=code.Escape(edmProperty)#> = _cache.<#=code.FieldName(edmProperty)#>;
        else
            OnPropertyChanged("<#=edmProperty.Name#>");
<#
            }
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call CancelEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).CancelEdit();
<#
            }
#>
        _cache = null;
    }

    void IEditableObject.EndEdit()
    {
        _cache = null;
<#
            if (complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // call EndEdit() on all Complex Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex))
            {
#>
        ((IEditableObject)<#=code.Escape(edmProperty)#>).EndEdit();
<#
            }
#>
    }
<#
        }
        region.End();
#>
}
<#
        EndNamespace(namespaceName);
#>
#endif
<#
    }
}  // End of Block for Complex Types

fileManager.Process();

#>
<#+
void WriteHeader(EntityFrameworkTemplateFileManager fileManager, params string[] extraUsings)
{
    fileManager.StartHeader();
#>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Xml.Linq;
<#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
    fileManager.EndBlock();
}

void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}

void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

void WriteEnumCollection()
{
#>
/// <summary>
/// Class EnumCollection (of T) creates an object which contains
/// a collection of every possible enum value of T where property Value
/// stores the actual enum value and DisplayName keeps the display name.
/// </summary>
/// <typeparam name="T">enum type</typeparam>
public class EnumCollection<T> : ICollection where T : struct
{
    #region "EnumItem class"

    public class EnumItem : INotifyPropertyChanged
    {
        public string DisplayName
        {
            get { return _displayName; }
            set
            {
                _displayName = value;
                OnPropertyChanged("DisplayName");
            }
        }

        private string _displayName;

        public string ResourceEntryKey { get; set; }

        public T? Value
        {
            get { return _value; }
            set
            {
                _value = value;
                OnPropertyChanged("Value");
            }
        }

        private T? _value;

        private void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    #endregion

    #region "Private Data Member"

    private readonly Collection<EnumItem> enumItems;
    private readonly ResourceManager resourceManager;

    #endregion "Private Data Member"

    #region "Constructor"

    /// <summary>
    /// Static constructor to make sure that T is of Enum type
    /// </summary>
    static EnumCollection()
    {
        if (!typeof (T).IsEnum)
            throw new ArgumentException("T must be an enumerated type");
    }

    /// <summary>
    /// Constructor to initialize EnumCollection
    /// </summary>
    /// <param name="firstAsNull">True to add the first row as null</param>
    /// <param name="resourceManager">ResourceManager where DisplayName gets its value</param>
    public EnumCollection(bool firstAsNull = false, ResourceManager resourceManager = null)
    {
        enumItems = new Collection<EnumItem>();
        this.resourceManager = resourceManager;

        if (firstAsNull)
            enumItems.Add(new EnumItem
                              {
                                  DisplayName = string.Empty,
                                  ResourceEntryKey = string.Empty,
                                  Value = null
                              });

        var type = typeof (T);
        foreach (var enumValue in (from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
                                   where field.IsLiteral
                                   select (T) field.GetValue(null)))
        {
            if (resourceManager != null)
            {
                var resourceEntryKey = string.Format("{0}_{1}", typeof (T).Name, enumValue);
                var displayName = resourceManager.GetString(resourceEntryKey);
                if (displayName != null)
                {
                    enumItems.Add(new EnumItem
                                      {
                                          DisplayName = displayName,
                                          ResourceEntryKey = resourceEntryKey,
                                          Value = enumValue
                                      });
                }
                else
                {
                    enumItems.Add(new EnumItem
                                      {
                                          DisplayName = string.Format("{0}_{1}", typeof (T).Name, enumValue),
                                          ResourceEntryKey = string.Empty,
                                          Value = enumValue
                                      });
                }
            }
            else
            {
                enumItems.Add(new EnumItem
                                  {
                                      DisplayName = string.Format("{0}", enumValue),
                                      ResourceEntryKey = string.Empty,
                                      Value = enumValue
                                  });
            }
        }
    }

    #endregion

    #region "Public Method & Property"

    /// <summary>
    /// Refreshes the DisplayName property for every item
    /// where the ResourceEntryKey property is not empty
    /// </summary>
    public void Refresh()
    {
        if (resourceManager == null) return;
        foreach (var item in enumItems.Where(n => !string.IsNullOrEmpty(n.ResourceEntryKey)))
        {
            var displayName = resourceManager.GetString(item.ResourceEntryKey);
            if (displayName != null) item.DisplayName = displayName;
        }
    }

    public Collection<EnumItem> Items
    {
        get { return enumItems; }
    }

    #endregion

    #region "ICollectionView implementation"

    public void CopyTo(Array array, int index)
    {
        ((ICollection) enumItems).CopyTo(array, index);
    }

    public int Count
    {
        get { return enumItems.Count; }
    }

    public bool IsSynchronized
    {
        get { return ((ICollection) enumItems).IsSynchronized; }
    }

    public object SyncRoot
    {
        get { return ((ICollection) enumItems).SyncRoot; }
    }

    public IEnumerator GetEnumerator()
    {
        return ((ICollection) enumItems).GetEnumerator();
    }

    #endregion
}

<#+
}

void WriteNotifyValidationResultChanged()
{
#>
public class ValidationResultChangedEventArgs : EventArgs
{
    public ValidationResultChangedEventArgs(string propertyName, IList<ValidationResult> removedItems, IList<ValidationResult> addedItems)
    {
        PropertyName = propertyName;
        RemovedItems = removedItems ?? new List<ValidationResult>();
        AddedItems = addedItems ?? new List<ValidationResult>();
    }

    public string PropertyName { get; private set; }
    public IList<ValidationResult> AddedItems { get; private set; }
    public IList<ValidationResult> RemovedItems { get; private set; }
}

// An interface that provides an event that fires when validation results changed
public interface INotifyValidationResultChanged
{
    event EventHandler<ValidationResultChangedEventArgs> ValidationResultChanged;
}
<#+
}

void WriteINodeLevelInterface()
{
#>

// An interface that defines the only method GetNextLevelNodes()
// which computes the level of a node inside the object graph
public interface INodeLevel
{
    List<INodeLevel> GetNextLevelNodes(Dictionary<INodeLevel, int> nodeLevelDictionary);
}
<#+
}

void WriteClientChangeTracking()
{
#>

// An interface that provides methods and properties for client side change tracking
public interface IClientChangeTracking
{
    // accepts changes made to the entity object
    void AcceptChanges();
    // rejects changes made to the entity object
    void RejectChanges();
    // whether the entity object has any changes
    bool HasChanges { get; }
    // accepts changes made to the entity object and all objects of its object graph
    void AcceptObjectGraphChanges();
    // rejects changes made to the entity object and all objects of its object graph
    void RejectObjectGraphChanges();
    // returns whether the entity object along with its object graph has any changes
    bool ObjectGraphHasChanges();
    // returns the estimate size of the entity object along with its object graph
    long EstimateObjectGraphSize();
    // returns the estimate size of the optimized entity object graph with only objects that have changes
    long EstimateObjectGraphChangeSize();
    // returns an optimized entity object graph with only objects that have changes
    IObjectWithChangeTracker GetObjectGraphChanges();
}
<#+
}

void WriteINotifyDataErrorInfoInterfaceForEntityType(EntityType entity)
{
    if (entity.BaseType == null)
    {
#>

    private Dictionary<string, List<ValidationResult>> _validationErrors;

    protected Dictionary<string, List<ValidationResult>> ValidationErrors
    {
        get { return _validationErrors ?? (_validationErrors = new Dictionary<string, List<ValidationResult>>()); }
    }

    protected event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
    /// <summary>
    /// Raises the INotifyDataErrorInfo.ErrorsChanged event
    /// </summary>
    protected void RaiseErrorsChanged(string propertyName)
    {
        if (ErrorsChanged != null)
        {
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
        }
    }
<#+
    }
#>

    event EventHandler<DataErrorsChangedEventArgs> INotifyDataErrorInfo.ErrorsChanged
    {
        add
        {
            ErrorsChanged += value;
        }
        remove
        {
            ErrorsChanged -= value;
        }
    }

    /// <summary>
    /// Gets the currently known errors for the provided property name. Use String.Empty/null
    /// to retrieve entity-level errors.
    /// </summary>
    IEnumerable INotifyDataErrorInfo.GetErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            return ValidationErrors[propertyName];
        }
        return null;
    }

    /// <summary>
    /// Gets a value indicating whether there are known errors or not.
    /// </summary>
    bool INotifyDataErrorInfo.HasErrors
    {
        get
        {
            return ValidationErrors.Keys.Count != 0;
        }
    }
<#+
}

void WriteINotifyDataErrorInfoInterfaceForComplexType()
{
#>

    private Dictionary<string, List<ValidationResult>> _validationErrors;

    protected Dictionary<string, List<ValidationResult>> ValidationErrors
    {
        get { return _validationErrors ?? (_validationErrors = new Dictionary<string, List<ValidationResult>>()); }
    }

    protected event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
    /// <summary>
    /// Raises the INotifyDataErrorInfo.ErrorsChanged event
    /// </summary>
    protected void RaiseErrorsChanged(string propertyName)
    {
        if (ErrorsChanged != null)
        {
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
        }
    }

    event EventHandler<DataErrorsChangedEventArgs> INotifyDataErrorInfo.ErrorsChanged
    {
        add
        {
            ErrorsChanged += value;
        }
        remove
        {
            ErrorsChanged -= value;
        }
    }

    /// <summary>
    /// Gets the currently known errors for the provided property name. Use String.Empty/null
    /// to retrieve entity-level errors.
    /// </summary>
    IEnumerable INotifyDataErrorInfo.GetErrors(string propertyName)
    {
        if (propertyName == null)
        {
            propertyName = string.Empty;
        }

        if (ValidationErrors.ContainsKey(propertyName))
        {
            return ValidationErrors[propertyName];
        }
        return null;
    }

    /// <summary>
    /// Gets a value indicating whether there are known errors or not.
    /// </summary>
    bool INotifyDataErrorInfo.HasErrors
    {
        get
        {
            return ValidationErrors.Keys.Count != 0;
        }
    }
<#+
}

void WriteIDataErrorInfoInterfaceForEntityType(EntityType entity)
{
    if (entity.BaseType == null)
    {
#>

    private Dictionary<string, ValidationResult> _validationErrors;

    protected Dictionary<string, ValidationResult> ValidationErrors
    {
        get
        {
            if (_validationErrors == null)
            {
                _validationErrors = new Dictionary<string, ValidationResult>();
            }
            return _validationErrors;
        }
    }
<#+
    }
#>

    string IDataErrorInfo.Error
    {
        get
        {
            if (ValidationErrors.ContainsKey(string.Empty))
            {
                return ValidationErrors[string.Empty].ErrorMessage;
            }
            return null;
        }
    }

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            if (propertyName == null)
            {
                propertyName = string.Empty;
            }

            if (ValidationErrors.ContainsKey(propertyName))
            {
                return ValidationErrors[propertyName].ErrorMessage;
            }
            return null;
        }
    }
<#+
}

void WriteIDataErrorInfoInterfaceForComplexType()
{
#>

    private Dictionary<string, ValidationResult> _validationErrors;

    protected Dictionary<string, ValidationResult> ValidationErrors
    {
        get
        {
            if (_validationErrors == null)
            {
                _validationErrors = new Dictionary<string, ValidationResult>();
            }
            return _validationErrors;
        }
    }

    string IDataErrorInfo.Error
    {
        get
        {
            if (ValidationErrors.ContainsKey(string.Empty))
            {
                return ValidationErrors[string.Empty].ErrorMessage;
            }
            return null;
        }
    }

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            if (propertyName == null)
            {
                propertyName = string.Empty;
            }

            if (ValidationErrors.ContainsKey(propertyName))
            {
                return ValidationErrors[propertyName].ErrorMessage;
            }
            return null;
        }
    }
<#+
}

void WriteIRevertibleChangeTrackingInterface()
{
#>

    void IChangeTracking.AcceptChanges()
    {
        this.AcceptChanges();
    }

    bool IChangeTracking.IsChanged
    {
        get { return HasChanges; }
    }

    void IRevertibleChangeTracking.RejectChanges()
    {
        this.RejectChanges();
    }
<#+
}

void WriteAcceptChangesAndRejectChanges()
{
#>

    /// <summary>
    /// Accepts changes made to the entity object
    /// </summary>
    void IClientChangeTracking.AcceptChanges()
    {
        this.AcceptChanges();
    }

    /// <summary>
    /// Rejects changes made to the entity object
    /// </summary>
    void IClientChangeTracking.RejectChanges()
    {
        this.RejectChanges();
    }
<#+
}

void WriteHasChangesProperty()
{
#>

    /// <summary>
    /// Returns whether the entity object has any changes
    /// </summary>
    public Boolean HasChanges
    {
        get { return _hasChanges; }
        private set
        {
            if (_hasChanges != value)
            {
                _hasChanges = value;
                if (_propertyChanged != null)
                {
                    _propertyChanged(this, new PropertyChangedEventArgs("HasChanges"));
                }
            }
        }
    }
    private Boolean _hasChanges = true;
<#+
}

void WriteINotifyValidationResultChangedInterface()
{
#>

    private void OnValidationResultChanged(string propertyName, IList<ValidationResult> removedItems, IList<ValidationResult> addedItems)
    {
        if (_validationResultChanged != null)
        {
            _validationResultChanged(this, new ValidationResultChangedEventArgs(propertyName, removedItems, addedItems));
        }
    }

    event EventHandler<ValidationResultChangedEventArgs> INotifyValidationResultChanged.ValidationResultChanged
    {
        add { _validationResultChanged += value; }
        remove { _validationResultChanged -= value; }
    }
    private event EventHandler<ValidationResultChangedEventArgs> _validationResultChanged;
<#+
}

void WriteIClientQueryOfT()
{
#>

/// <summary>
/// Interface IClientQuery( of T)
/// </summary>
/// <typeparam name="T">Expected type of the IClientQuery</typeparam>
public interface IClientQuery<T>
{
    ClientQuery AsClientQuery();
    ClientQuery<T> Include(string path);
    ClientQuery<T> Include<TKey>(Expression<Func<T, TKey>> propertySelector) where TKey : class, INotifyPropertyChanged;
    ClientQuery<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector);
    ClientQuery<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> keySelector);
    ClientQuery<T> Skip(int count);
    ClientQuery<T> Take(int count);
    ClientQuery<T> ThenBy<TKey>(Expression<Func<T, TKey>> keySelector);
    ClientQuery<T> ThenByDescending<TKey>(Expression<Func<T, TKey>> keySelector);
    ClientQuery<T> Where(Expression<Func<T, bool>> predicate);
    ClientQuery<T> ApplyClientFilter(ClientFilter<T> clientFilter);
}
<#+
}

void WriteClientQueryOfT()
{
#>

/// <summary>
/// Implementation of interface IClientQuery( of T)
/// </summary>
/// <typeparam name="T">Expected type of the ClientQuery</typeparam>
public class ClientQuery<T> : IClientQuery<T>
{
    #region Private Data Member

    private readonly ClientQueryInternal<T> _clientQueryInternal;
    private readonly Serializer _serializer;
    private readonly List<string> _includeList;
    private readonly XElement _xmlExpression;
    private static readonly MethodInfo[] IncludeMethods;

    #endregion Private Data Member

    #region Constructor

    static ClientQuery()
    {
        var type = typeof (ClientQueryExtension);
        IncludeMethods = type.GetMethods().Where(n => n.Name == "Include").ToArray();
    }

    public ClientQuery()
    {
        _clientQueryInternal = new ClientQueryInternal<T>();
        _serializer = new Serializer();
        _includeList = new List<string>();
        _xmlExpression = _serializer.Serialize(_clientQueryInternal.Expression);
    }

    private ClientQuery(List<string> includeList, ClientQueryInternal<T> clientQueryInternal)
    {
        _clientQueryInternal = clientQueryInternal;
        _serializer = new Serializer();
        _includeList = includeList;
        _xmlExpression = _serializer.Serialize(_clientQueryInternal.Expression);
    }

    #endregion Constructor

    #region Interface IClientQuery<T> Implementation

    public ClientQuery AsClientQuery()
    {
        return new ClientQuery(_includeList, _xmlExpression);
    }

    public ClientQuery<T> Include(string path)
    {
        if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Invalid path argument.");
        _includeList.Add(path);
        return new ClientQuery<T>(_includeList, _clientQueryInternal);
    }

    public ClientQuery<T> Include<TKey>(Expression<Func<T, TKey>> propertySelector)
        where TKey : class, INotifyPropertyChanged
    {
        if (propertySelector == null) throw new ArgumentException("Invalid propertySelector argument");
        var includeString = BuildString(propertySelector);
        _includeList.Add(includeString);
        return new ClientQuery<T>(_includeList, _clientQueryInternal);
    }

    public ClientQuery<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(_clientQueryInternal.OrderBy(keySelector).Expression));
    }

    public ClientQuery<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(
                                      _clientQueryInternal.OrderByDescending(keySelector).Expression));
    }

    public ClientQuery<T> Skip(int count)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(_clientQueryInternal.Skip(count).Expression));
    }

    public ClientQuery<T> Take(int count)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(_clientQueryInternal.Take(count).Expression));
    }

    public ClientQuery<T> ThenBy<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(_clientQueryInternal.ThenBy(keySelector).Expression));
    }

    public ClientQuery<T> ThenByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(
                                      _clientQueryInternal.ThenByDescending(keySelector).Expression));
    }

    public ClientQuery<T> Where(Expression<Func<T, bool>> predicate)
    {
        return new ClientQuery<T>(_includeList,
                                  new ClientQueryInternal<T>(_clientQueryInternal.Where(predicate).Expression));
    }

    public ClientQuery<T> ApplyClientFilter(ClientFilter<T> clientFilter)
    {
        ClientQuery<T> clientQuery = this;
        if (clientFilter == null) return clientQuery;

        if (clientFilter.WhereExpression != null)
        {
            clientQuery =
                clientQuery.Where((Expression<Func<T, bool>>)
                                  new RemoveInvokeVisitor().Visit(clientFilter.WhereExpression));
        }

        if (clientFilter.SortExpressions.Count > 0)
        {
            for (var i = 0; i < clientFilter.SortExpressions.Count; i++)
            {
                if (i == 0)
                {
                    if (clientFilter.SortExpressions[i].SortDirection == ListSortDirection.Ascending)
                    {
                        MethodInfo methodInfo =
                            typeof (ClientQuery<T>).GetMethod("OrderBy").MakeGenericMethod(
                                clientFilter.SortExpressions[i].KeyType);

                        clientQuery = (ClientQuery<T>) methodInfo.Invoke(clientQuery,
                                                                         new object[]
                                                                             {
                                                                                 clientFilter.SortExpressions[i].
                                                                             KeySelector
                                                                             });
                    }
                    else if (clientFilter.SortExpressions[i].SortDirection == ListSortDirection.Descending)
                    {
                        MethodInfo methodInfo =
                            typeof (ClientQuery<T>).GetMethod("OrderByDescending").MakeGenericMethod(
                                clientFilter.SortExpressions[i].KeyType);

                        clientQuery = (ClientQuery<T>) methodInfo.Invoke(clientQuery,
                                                                         new object[]
                                                                             {
                                                                                 clientFilter.SortExpressions[i].
                                                                             KeySelector
                                                                             });
                    }
                }
                else
                {
                    if (clientFilter.SortExpressions[i].SortDirection == ListSortDirection.Ascending)
                    {
                        MethodInfo methodInfo =
                            typeof (ClientQuery<T>).GetMethod("ThenBy").MakeGenericMethod(
                                clientFilter.SortExpressions[i].KeyType);

                        clientQuery = (ClientQuery<T>) methodInfo.Invoke(clientQuery,
                                                                         new object[]
                                                                             {
                                                                                 clientFilter.SortExpressions[i].
                                                                             KeySelector
                                                                             });
                    }
                    else if (clientFilter.SortExpressions[i].SortDirection == ListSortDirection.Descending)
                    {
                        MethodInfo methodInfo =
                            typeof (ClientQuery<T>).GetMethod("ThenByDescending").MakeGenericMethod(
                                clientFilter.SortExpressions[i].KeyType);

                        clientQuery = (ClientQuery<T>) methodInfo.Invoke(clientQuery,
                                                                         new object[]
                                                                             {
                                                                                 clientFilter.SortExpressions[i].
                                                                             KeySelector
                                                                             });
                    }
                }
            }
        }

        if (clientFilter.SkipCount != null)
            clientQuery = clientQuery.Skip((int) clientFilter.SkipCount);

        if (clientFilter.TakeCount != null)
            clientQuery = clientQuery.Take((int) clientFilter.TakeCount);

        return clientQuery;
    }

    #endregion Interface IClientQuery<T> Implementation

    #region Private Method

    private static bool IsInclude(MethodInfo methodInfo)
    {
        if (methodInfo.IsGenericMethod && !methodInfo.IsGenericMethodDefinition)
            methodInfo = methodInfo.GetGenericMethodDefinition();
        return IncludeMethods.Contains(methodInfo);
    }

    private static string BuildString(Expression propertySelector)
    {
        switch (propertySelector.NodeType)
        {
            case ExpressionType.Lambda:
                var lambdaExpression = (LambdaExpression) propertySelector;
                return BuildString(lambdaExpression.Body);

            case ExpressionType.Quote:
                var unaryExpression = (UnaryExpression) propertySelector;
                return BuildString(unaryExpression.Operand);

            case ExpressionType.MemberAccess:
                var memberExpression = (MemberExpression) propertySelector;
                var propertyInfo = memberExpression.Member;

                if (memberExpression.Expression is ParameterExpression)
                    return propertyInfo.Name;
                // we've got a nested property (e.g. MyType.SomeProperty.SomeNestedProperty)
                return BuildString(memberExpression.Expression) + "." + propertyInfo.Name;

            case ExpressionType.Call:
                var methodCallExpression = (MethodCallExpression) propertySelector;
                if (IsInclude(methodCallExpression.Method)) // check that it's a Include call
                {
                    // argument 0 is the expression to which the Include is applied (this could be member access or another Include)
                    // argument 1 is the expression to apply to get the included property
                    // Pass both to BuildString to get the full expression
                    return BuildString(methodCallExpression.Arguments[0]) + "." +
                           BuildString(methodCallExpression.Arguments[1]);
                }
                // else drop out and throw
                break;
        }
        throw new InvalidOperationException("Expression must be a member expression or an Include call: " +
                                            propertySelector);
    }

    #endregion Private Method

    private class ClientQueryInternal<TChild> : IOrderedQueryable<TChild>
    {
        #region Private Data Member

        private readonly IQueryProvider _provider;
        private readonly Expression _expression;

        #endregion Private Data Member

        #region Constructor

        public ClientQueryInternal()
        {
            _provider = new List<TChild>().AsQueryable().Provider;
            _expression = Expression.Constant(this);
        }

        public ClientQueryInternal(Expression expression)
        {
            _provider = new List<TChild>().AsQueryable().Provider;
            _expression = expression;
        }

        #endregion Constructor

        #region IOrderedQueryable<TChild> Interface Implementation

        public IEnumerator<TChild> GetEnumerator()
        {
            return ((IEnumerable<TChild>) _provider.Execute(_expression)).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable) _provider.Execute(_expression)).GetEnumerator();
        }

        public Type ElementType
        {
            get { return typeof (TChild); }
        }

        public Expression Expression
        {
            get { return _expression; }
        }

        public IQueryProvider Provider
        {
            get { return _provider; }
        }

        #endregion IOrderedQueryable<TChild> Interface Implementation
    }
}

public static class ClientQueryExtension
{
    public static TNaviProperty Include<TEntity, TNaviProperty>(this TrackableCollection<TEntity> source,
                                                                Expression<Func<TEntity, TNaviProperty>>
                                                                    propertySelector)
        where TEntity : class, IObjectWithChangeTracker
        where TNaviProperty : class, INotifyPropertyChanged
    {
        throw new NotImplementedException();
    }

    public static TNaviProperty Include<TEntity, TNaviProperty>(this TEntity source,
                                                                Expression<Func<TEntity, TNaviProperty>>
                                                                    propertySelector)
        where TEntity : class, IObjectWithChangeTracker
        where TNaviProperty : class, INotifyPropertyChanged
    {
        throw new NotImplementedException();
    }
}
<#+
}

void WriteClientFilterOfT()
{
#>

/// <summary>
/// A class that stores dynamically built expressions for filtering, paging, and sorting
/// </summary>
/// <typeparam name="T">Expected type of the ClientFilter</typeparam>
public class ClientFilter<T>
{
    #region Private Data Member

    private Expression<Func<T, bool>> _whereExpression;
    private readonly List<SortExpression> _sortExpressions;

    #endregion Private Data Member

    #region Public Property

    public Expression<Func<T, bool>> WhereExpression
    {
        get { return _whereExpression; }
        private set { _whereExpression = value != null ? (Expression<Func<T, bool>>) Evaluator.PartialEval(value) : value; }
    }

    public ReadOnlyCollection<SortExpression> SortExpressions
    {
        get { return _sortExpressions.AsReadOnly(); }
    }

    public int? SkipCount { get; private set; }

    public int? TakeCount { get; private set; }

    #endregion Public Property

    #region Constructor

    public ClientFilter()
    {
        WhereExpression = null;
        _sortExpressions = new List<SortExpression>();
        SkipCount = null;
        TakeCount = null;
    }

    public ClientFilter(Expression<Func<T, bool>> whereExpression, List<SortExpression> sortExpressions,
                        int? skipCount, int? takeCount)
    {
        WhereExpression = whereExpression;
        if (sortExpressions != null)
            _sortExpressions = sortExpressions;
        else
            _sortExpressions = new List<SortExpression>();
        SkipCount = skipCount;
        TakeCount = takeCount;
    }

    #endregion Constructor

    #region Public Method

    public ClientFilter<T> And(Expression<Func<T, bool>> whereExpression)
    {
        if (WhereExpression == null) WhereExpression = n => true;
        if (whereExpression != null)
        {
            var invokedExpr = Expression.Invoke(whereExpression, WhereExpression.Parameters.ToArray());
            WhereExpression = Expression.Lambda<Func<T, bool>>(
                Expression.AndAlso(WhereExpression.Body, invokedExpr), WhereExpression.Parameters);
        }
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Or(Expression<Func<T, bool>> whereExpression)
    {
        if (WhereExpression == null) WhereExpression = n => false;
        if (whereExpression != null)
        {
            var invokedExpr = Expression.Invoke(whereExpression, WhereExpression.Parameters.ToArray());
            WhereExpression = Expression.Lambda<Func<T, bool>>(
                Expression.OrElse(WhereExpression.Body, invokedExpr), WhereExpression.Parameters);
        }
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        _sortExpressions.Add(new SortExpression(keySelector, typeof(TKey) ,ListSortDirection.Ascending));
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        _sortExpressions.Add(new SortExpression(keySelector, typeof (TKey), ListSortDirection.Descending));
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Skip(int count)
    {
        SkipCount = count;
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Take(int count)
    {
        TakeCount = count;
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public override string ToString()
    {
        var serializer = new Serializer();
        var xElementString = new XElement(
            "ClientFilter",
            new XAttribute("Type", typeof (T).FullName),
            WhereExpression == null
                ? new XElement("WhereExpression")
                : new XElement("WhereExpression", serializer.Serialize(WhereExpression).ToString()),
            new XElement("SortExpressions",
                         from sortExpression in SortExpressions
                         select
                             new XElement("SortExpression",
                                          new XElement("KeySelector",
                                                       serializer.Serialize(
                                                           sortExpression.KeySelector).ToString()),
                                          new XElement("KeyType", sortExpression.KeyType.FullName),
                                          new XElement("SortDirection", sortExpression.SortDirection))),
            new XElement("SkipCount", SkipCount != null ? SkipCount.ToString() : string.Empty),
            new XElement("TakeCount", TakeCount != null ? TakeCount.ToString() : string.Empty)).ToString();
        var byteArray = System.Text.Encoding.UTF8.GetBytes(xElementString);
        return Convert.ToBase64String(byteArray);
    }

    public static ClientFilter<T> Parse(string base64String)
    {
        var byteArray = Convert.FromBase64String(base64String);
        var xmlString = System.Text.Encoding.UTF8.GetString(byteArray);
        XElement xElement = XElement.Parse(xmlString);
        if (xElement.Name.LocalName == "ClientFilter" && xElement.Attribute("Type") != null)
        {
            if (Type.GetType(xElement.Attribute("Type").Value) == typeof (T))
            {
                var whereExpression = ParseWhereExpression(xElement.Element("WhereExpression"));
                var sortExpressions = ParseSortExpressions(xElement.Element("SortExpressions"));
                var skipCount = ParseNullableInt(xElement.Element("SkipCount"));
                var takeCount = ParseNullableInt(xElement.Element("TakeCount"));
                return new ClientFilter<T>(whereExpression, sortExpressions, skipCount, takeCount);
            }
            throw new ArgumentException("Invalid ClientFilter type.");
        }
        return null;
    }

    #endregion Public Method

    #region Private Method

    private static Expression<Func<T, bool>> ParseWhereExpression(XElement xml)
    {
        if (!string.IsNullOrEmpty(xml.Value))
            return (Expression<Func<T, bool>>) new Deserializer().Deserialize(XElement.Parse(xml.Value));
        return null;
    }

    private static List<SortExpression> ParseSortExpressions(XElement xml)
    {
        var sortExpressions = new List<SortExpression>();
        var deSerializer = new Deserializer();
        sortExpressions.AddRange(from element in xml.Elements("SortExpression")
                                 let keySelector = element.Element("KeySelector")
                                 let keyType = element.Element("KeyType")
                                 let sortDirection = element.Element("SortDirection")
                                 where keySelector != null && sortDirection != null
                                 select new SortExpression(
                                     deSerializer.Deserialize(XElement.Parse(keySelector.Value)),
                                     Type.GetType(keyType.Value),
                                     (ListSortDirection)
                                     Enum.Parse(typeof (ListSortDirection), sortDirection.Value, false)));
        return sortExpressions;
    }

    private static int? ParseNullableInt(XElement xml)
    {
        if (!string.IsNullOrEmpty(xml.Value))
            return int.Parse(xml.Value);
        return null;
    }

    #endregion Private Method
}
<#+
}

void WriteClientFilterOfTForWPF()
{
#>

/// <summary>
/// A class that stores dynamically built expressions for filtering, paging, and sorting
/// </summary>
/// <typeparam name="T">Expected type of the ClientFilter</typeparam>
public class ClientFilter<T>
{
    #region Private Data Member

    private Expression<Func<T, bool>> _whereExpression;
    private readonly List<SortExpression> _sortExpressions;

    #endregion Private Data Member

    #region Public Property

    public Expression<Func<T, bool>> WhereExpression
    {
        get { return _whereExpression; }
        private set { _whereExpression = value != null ? (Expression<Func<T, bool>>) Evaluator.PartialEval(value) : value; }
    }

    public ReadOnlyCollection<SortExpression> SortExpressions
    {
        get { return _sortExpressions.AsReadOnly(); }
    }

    public int? SkipCount { get; private set; }

    public int? TakeCount { get; private set; }

    #endregion Public Property

    #region Constructor

    public ClientFilter()
    {
        WhereExpression = null;
        _sortExpressions = new List<SortExpression>();
        SkipCount = null;
        TakeCount = null;
    }

    public ClientFilter(Expression<Func<T, bool>> whereExpression, List<SortExpression> sortExpressions,
                        int? skipCount, int? takeCount)
    {
        WhereExpression = whereExpression;
        if (sortExpressions != null)
            _sortExpressions = sortExpressions;
        else
            _sortExpressions = new List<SortExpression>();
        SkipCount = skipCount;
        TakeCount = takeCount;
    }

    #endregion Constructor

    #region Public Method

    public ClientFilter<T> And(Expression<Func<T, bool>> whereExpression)
    {
        if (WhereExpression == null) WhereExpression = n => true;
        if (whereExpression != null)
        {
            var invokedExpr = Expression.Invoke(whereExpression, WhereExpression.Parameters.ToArray());
            WhereExpression = Expression.Lambda<Func<T, bool>>(
                Expression.AndAlso(WhereExpression.Body, invokedExpr), WhereExpression.Parameters);
        }
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Or(Expression<Func<T, bool>> whereExpression)
    {
        if (WhereExpression == null) WhereExpression = n => false;
        if (whereExpression != null)
        {
            var invokedExpr = Expression.Invoke(whereExpression, WhereExpression.Parameters.ToArray());
            WhereExpression = Expression.Lambda<Func<T, bool>>(
                Expression.OrElse(WhereExpression.Body, invokedExpr), WhereExpression.Parameters);
        }
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        _sortExpressions.Add(new SortExpression(keySelector, typeof(TKey) ,ListSortDirection.Ascending));
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        _sortExpressions.Add(new SortExpression(keySelector, typeof (TKey), ListSortDirection.Descending));
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Skip(int count)
    {
        SkipCount = count;
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public ClientFilter<T> Take(int count)
    {
        TakeCount = count;
        return new ClientFilter<T>(WhereExpression, _sortExpressions, SkipCount, TakeCount);
    }

    public override string ToString()
    {
        var serializer = new Serializer();
        var xElement = new XElement(
            "ClientFilter",
            new XAttribute("Type", typeof (T).FullName),
            WhereExpression == null
                ? new XElement("WhereExpression")
                : new XElement("WhereExpression", serializer.Serialize(WhereExpression).ToString()),
            new XElement("SortExpressions",
                         from sortExpression in SortExpressions
                         select
                             new XElement("SortExpression",
                                          new XElement("KeySelector",
                                                       serializer.Serialize(
                                                           sortExpression.KeySelector).ToString()),
                                          new XElement("KeyType", sortExpression.KeyType.FullName),
                                          new XElement("SortDirection", sortExpression.SortDirection))),
            new XElement("SkipCount", SkipCount != null ? SkipCount.ToString() : string.Empty),
            new XElement("TakeCount", TakeCount != null ? TakeCount.ToString() : string.Empty));

        return CompressUtil.Compress(xElement);
    }

    public static ClientFilter<T> Parse(string base64String)
    {
        XElement xElement = CompressUtil.Decompress(base64String);

        if (xElement.Name.LocalName == "ClientFilter" && xElement.Attribute("Type") != null)
        {
            if (Type.GetType(xElement.Attribute("Type").Value) == typeof (T))
            {
                var whereExpression = ParseWhereExpression(xElement.Element("WhereExpression"));
                var sortExpressions = ParseSortExpressions(xElement.Element("SortExpressions"));
                var skipCount = ParseNullableInt(xElement.Element("SkipCount"));
                var takeCount = ParseNullableInt(xElement.Element("TakeCount"));
                return new ClientFilter<T>(whereExpression, sortExpressions, skipCount, takeCount);
            }
            throw new ArgumentException("Invalid ClientFilter type.");
        }
        return null;
    }

    #endregion Public Method

    #region Private Method

    private static Expression<Func<T, bool>> ParseWhereExpression(XElement xml)
    {
        if (!string.IsNullOrEmpty(xml.Value))
            return (Expression<Func<T, bool>>) new Deserializer().Deserialize(XElement.Parse(xml.Value));
        return null;
    }

    private static List<SortExpression> ParseSortExpressions(XElement xml)
    {
        var sortExpressions = new List<SortExpression>();
        var deSerializer = new Deserializer();
        sortExpressions.AddRange(from element in xml.Elements("SortExpression")
                                 let keySelector = element.Element("KeySelector")
                                 let keyType = element.Element("KeyType")
                                 let sortDirection = element.Element("SortDirection")
                                 where keySelector != null && sortDirection != null
                                 select new SortExpression(
                                     deSerializer.Deserialize(XElement.Parse(keySelector.Value)),
                                     Type.GetType(keyType.Value),
                                     (ListSortDirection)
                                     Enum.Parse(typeof (ListSortDirection), sortDirection.Value, false)));
        return sortExpressions;
    }

    private static int? ParseNullableInt(XElement xml)
    {
        if (!string.IsNullOrEmpty(xml.Value))
            return int.Parse(xml.Value);
        return null;
    }

    #endregion Private Method
}
<#+
}

void WriteSortExpression()
{
#>

public class SortExpression
{
    public Expression KeySelector { get; private set; }
    public Type KeyType { get; private set; }
    public ListSortDirection SortDirection { get; private set; }

    public SortExpression(Expression keySelector, Type keyType, ListSortDirection sortDirection)
    {
        KeySelector = keySelector;
        KeyType = keyType;
        SortDirection = sortDirection;
    }
}
<#+
}

void WriteRemoveInvokeVisitor()
{
#>

internal class RemoveInvokeVisitor : ExpressionVisitor
{
    protected override Expression VisitInvocation(InvocationExpression node)
    {
        var newPars = node.Arguments;
        var lambda = (node.Expression) as LambdaExpression;
        if (lambda != null)
        {
            var oldPars = lambda.Parameters;
            var replace = new ReplaceParametersVisitor(oldPars, newPars);
            return this.Visit(replace.Visit(lambda.Body));
        }
        return base.VisitInvocation(node);
    }
}

internal class ReplaceParametersVisitor : ExpressionVisitor
{
    private ReadOnlyCollection<ParameterExpression> _oldParameters;
    private ReadOnlyCollection<Expression> _newParameters;

    public ReplaceParametersVisitor(ReadOnlyCollection<ParameterExpression> oldParameters,
        ReadOnlyCollection<Expression> newParameters)
    {
        _oldParameters = oldParameters;
        _newParameters = newParameters;
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (_oldParameters != null && _newParameters != null)
        {
            if (_oldParameters.Contains(node))
                return _newParameters[_oldParameters.IndexOf(node)];
        }
        return base.VisitParameter(node);
    }
}
<#+
}

void WriteSerializer()
{
#>

internal class Serializer
{
    #region Public Serialize Method

    public XElement Serialize(Expression e)
    {
        e = Evaluator.PartialEval(e);
        return GenerateXmlFromExpressionCore(e);
    }

    #endregion Public Serialize Method

    #region Private Serializer Method

    private XElement GenerateXmlFromExpressionCore(Expression e)
    {
        if (e == null) return null;

        if (e.NodeType == ExpressionType.Constant && typeof (IOrderedQueryable).IsAssignableFrom(e.Type))
        {
            var elementType = ((IOrderedQueryable) ((ConstantExpression) e).Value).ElementType;
            if (typeof (IOrderedQueryable<>).MakeGenericType(new[] {elementType})
                    .IsAssignableFrom(e.Type) && elementType.FullName != null)
                return new XElement("ClientQuery",
                                    new XAttribute("elementType", elementType.FullName));
        }

        var binaryExpression = e as BinaryExpression;
        if (binaryExpression != null)
        {
            return BinaryExpressionToXElement(binaryExpression);
        }
        var conditionalExpression = e as ConditionalExpression;
        if (conditionalExpression != null)
        {
            return ConditionalExpressionToXElement(conditionalExpression);
        }
        var constantExpression = e as ConstantExpression;
        if (constantExpression != null)
        {
            return ConstantExpressionToXElement(constantExpression);
        }
        var invocationExpression = e as InvocationExpression;
        if (invocationExpression != null)
        {
            return InvocationExpressionToXElement(invocationExpression);
        }
        var lambdaExpression = e as LambdaExpression;
        if (lambdaExpression != null)
        {
            return LambdaExpressionToXElement(lambdaExpression);
        }
        var listInitExpression = e as ListInitExpression;
        if (listInitExpression != null)
        {
            return ListInitExpressionToXElement(listInitExpression);
        }
        var memberExpression = e as MemberExpression;
        if (memberExpression != null)
        {
            return MemberExpressionToXElement(memberExpression);
        }
        var memberInitExpression = e as MemberInitExpression;
        if (memberInitExpression != null)
        {
            return MemberInitExpressionToXElement(memberInitExpression);
        }
        var methodCallExpression = e as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return MethodCallExpressionToXElement(methodCallExpression);
        }
        var newArrayExpression = e as NewArrayExpression;
        if (newArrayExpression != null)
        {
            return NewArrayExpressionToXElement(newArrayExpression);
        }
        var newExpression = e as NewExpression;
        if (newExpression != null)
        {
            return NewExpressionToXElement(newExpression);
        }
        var parameterExpression = e as ParameterExpression;
        if (parameterExpression != null)
        {
            return ParameterExpressionToXElement(parameterExpression);
        }
        var typeBinaryExpression = e as TypeBinaryExpression;
        if (typeBinaryExpression != null)
        {
            return TypeBinaryExpressionToXElement(typeBinaryExpression);
        }
        var unaryExpression = e as UnaryExpression;
        if (unaryExpression != null)
        {
            return UnaryExpressionToXElement(unaryExpression);
        }
        throw new NotSupportedException("Expression not supported.");
    }

    private XElement BinaryExpressionToXElement(BinaryExpression e)
    {
        var xElementValues = new object[6];
        xElementValues[0] = GenerateXmlFromExpression("Right", e.Right);
        xElementValues[1] = GenerateXmlFromExpression("Left", e.Left);
        xElementValues[2] = GenerateXmlFromMethodInfo("Method", e.Method);
        xElementValues[3] = GenerateXmlFromExpression("Conversion", e.Conversion);
        xElementValues[4] = GenerateXmlWithDataContractSerializer(typeof (bool), "IsLiftedToNull", e.IsLiftedToNull);
        xElementValues[5] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("BinaryExpression", xElementValues);
    }

    private XElement ConditionalExpressionToXElement(ConditionalExpression e)
    {
        var xElementValues = new object[5];
        xElementValues[0] = GenerateXmlFromExpression("Test", e.Test);
        xElementValues[1] = GenerateXmlFromExpression("IfTrue", e.IfTrue);
        xElementValues[2] = GenerateXmlFromExpression("IfFalse", e.IfFalse);
        xElementValues[3] = GenerateXmlFromType("Type", e.Type);
        xElementValues[4] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("ConditionalExpression", xElementValues);
    }

    private XElement ConstantExpressionToXElement(ConstantExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlWithDataContractSerializer(e.Type, "Value", e.Value);
        xElementValues[1] = GenerateXmlFromType("Type", e.Type);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("ConstantExpression", xElementValues);
    }

    private XElement InvocationExpressionToXElement(InvocationExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromExpression("Expression", e.Expression);
        xElementValues[1] = GenerateXmlFromExpressionList("Arguments", e.Arguments);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("InvocationExpression", xElementValues);
    }

    private XElement LambdaExpressionToXElement(LambdaExpression e)
    {
        var xElementValues = new object[6];
        xElementValues[0] = GenerateXmlFromType("Type", e.Type);
        xElementValues[1] = GenerateXmlFromExpression("Body", e.Body);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (string), "Name", e.Name);
        xElementValues[3] = GenerateXmlWithDataContractSerializer(typeof (bool), "TailCall", e.TailCall);
        xElementValues[4] = GenerateXmlFromExpressionList("Parameters", AsIEnumerableOf<Expression>(e.Parameters));
        xElementValues[5] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("LambdaExpression", xElementValues);
    }

    private XElement ListInitExpressionToXElement(ListInitExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromExpression("NewExpression", e.NewExpression);
        xElementValues[1] = GenerateXmlFromElementInitList("Initializers", e.Initializers);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("ListInitExpression", xElementValues);
    }

    private XElement MemberExpressionToXElement(MemberExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromExpression("Expression", e.Expression);
        xElementValues[1] = GenerateXmlFromMemberInfo(typeof (MemberInfo), "Member", e.Member);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("MemberExpression", xElementValues);
    }

    private XElement MemberInitExpressionToXElement(MemberInitExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromExpression("NewExpression", e.NewExpression);
        xElementValues[1] = GenerateXmlFromMemberBindingList("Bindings", e.Bindings);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("MemberInitExpression", xElementValues);
    }

    private XElement MethodCallExpressionToXElement(MethodCallExpression e)
    {
        var xElementValues = new object[4];
        xElementValues[0] = GenerateXmlFromExpression("Object", e.Object);
        xElementValues[1] = GenerateXmlFromMethodInfo("Method", e.Method);
        xElementValues[2] = GenerateXmlFromExpressionList("Arguments", e.Arguments);
        xElementValues[3] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("MethodCallExpression", xElementValues);
    }

    private XElement NewArrayExpressionToXElement(NewArrayExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromType("Type", e.Type);
        xElementValues[1] = GenerateXmlFromExpressionList("Expressions", e.Expressions);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("NewArrayExpression", xElementValues);
    }

    private XElement NewExpressionToXElement(NewExpression e)
    {
        var xElementValues = new object[4];
        xElementValues[0] = GenerateXmlFromConstructorInfo("Constructor", e.Constructor);
        xElementValues[1] = GenerateXmlFromExpressionList("Arguments", e.Arguments);
        xElementValues[2] = GenerateXmlFromMemberInfoList("Members", e.Members);
        xElementValues[3] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("NewExpression", xElementValues);
    }

    private XElement ParameterExpressionToXElement(ParameterExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromType("Type", e.Type);
        xElementValues[1] = GenerateXmlWithDataContractSerializer(typeof (string), "Name", e.Name);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("ParameterExpression", xElementValues);
    }

    private XElement TypeBinaryExpressionToXElement(TypeBinaryExpression e)
    {
        var xElementValues = new object[3];
        xElementValues[0] = GenerateXmlFromExpression("Expression", e.Expression);
        xElementValues[1] = GenerateXmlFromType("TypeOperand", e.TypeOperand);
        xElementValues[2] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("TypeBinaryExpression", xElementValues);
    }

    private XElement UnaryExpressionToXElement(UnaryExpression e)
    {
        var xElementValues = new object[4];
        xElementValues[0] = GenerateXmlFromExpression("Operand", e.Operand);
        xElementValues[1] = GenerateXmlFromType("Type", e.Type);
        xElementValues[2] = GenerateXmlFromMethodInfo("Method", e.Method);
        xElementValues[3] = GenerateXmlWithDataContractSerializer(typeof (ExpressionType), "NodeType", e.NodeType);
        return new XElement("UnaryExpression", xElementValues);
    }

    #endregion Private Serializer Method

    #region Private Serializer Helper Method

    private XElement GenerateXmlWithDataContractSerializer(Type propType, string propName, object value)
    {
        using (var stream = new MemoryStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            var serializer = new DataContractSerializer(propType);
            serializer.WriteObject(stream, value);
            stream.Seek(0, SeekOrigin.Begin);
            return new XElement(propName, reader.ReadToEnd());
        }
    }

    private XElement GenerateXmlFromConstructorInfo(string propName, ConstructorInfo constructorInfo)
    {
        return constructorInfo == null
                   ? new XElement(propName)
                   : new XElement(propName,
                                  new XAttribute("MemberType", constructorInfo.MemberType),
                                  new XAttribute("MethodName", constructorInfo.Name),
                                  GenerateXmlFromType("DeclaringType", constructorInfo.DeclaringType),
                                  new XElement("Parameters",
                                               from param in constructorInfo.GetParameters()
                                               select new XElement("Parameter",
                                                                   new XAttribute("Name", param.Name),
                                                                   GenerateXmlFromType("Type", param.ParameterType))));
    }

    private XElement GenerateXmlFromElementInit(ElementInit elementInit)
    {
        return new XElement("ElementInit",
                            GenerateXmlFromMethodInfo("AddMethod", elementInit.AddMethod),
                            GenerateXmlFromExpressionList("Arguments", elementInit.Arguments));
    }

    private XElement GenerateXmlFromExpression(string propName, Expression e)
    {
        return new XElement(propName, GenerateXmlFromExpressionCore(e));
    }

    private XElement GenerateXmlFromFieldInfo(string propName, FieldInfo fieldInfo)
    {
        return fieldInfo == null
                   ? new XElement(propName)
                   : new XElement(propName,
                                  new XAttribute("MemberType", fieldInfo.MemberType),
                                  new XAttribute("FieldName", fieldInfo.Name),
                                  GenerateXmlFromType("DeclaringType", fieldInfo.DeclaringType));
    }

    private XElement GenerateXmlFromMemberAssignment(MemberAssignment memberAssignment)
    {
        return new XElement("MemberAssignment",
                            GenerateXmlFromMemberInfo(memberAssignment.Member.GetType(), "Member",
                                                      memberAssignment.Member),
                            GenerateXmlFromExpression("Expression", memberAssignment.Expression));
    }

    private XElement GenerateXmlFromMemberBinding(MemberBinding binding)
    {
        switch (binding.BindingType)
        {
            case MemberBindingType.Assignment:
                return GenerateXmlFromMemberAssignment(binding as MemberAssignment);
            case MemberBindingType.ListBinding:
                return GenerateXmlFromMemberListBinding(binding as MemberListBinding);
            case MemberBindingType.MemberBinding:
                return GenerateXmlFromMemberMemberBinding(binding as MemberMemberBinding);
            default:
                throw new NotSupportedException(string.Format("Binding type {0} not supported.", binding.BindingType));
        }
    }

    private XElement GenerateXmlFromMemberListBinding(MemberListBinding memberListBinding)
    {
        return new XElement("MemberListBinding",
                            GenerateXmlFromMemberInfo(memberListBinding.Member.GetType(), "Member",
                                                      memberListBinding.Member),
                            GenerateXmlFromElementInitList("Initializers", memberListBinding.Initializers));
    }

    private XElement GenerateXmlFromMemberMemberBinding(MemberMemberBinding memberMemberBinding)
    {
        return new XElement("MemberMemberBinding",
                            GenerateXmlFromMemberInfo(memberMemberBinding.Member.GetType(), "Member",
                                                      memberMemberBinding.Member),
                            GenerateXmlFromMemberBindingList("Bindings", memberMemberBinding.Bindings));
    }

    private XElement GenerateXmlFromMemberInfo(Type propType, string propName, MemberInfo value)
    {
        if (propType == typeof (ConstructorInfo) || value is ConstructorInfo)
            return GenerateXmlFromConstructorInfo(propName, value as ConstructorInfo);
        if (propType == typeof (FieldInfo) || value is FieldInfo)
            return GenerateXmlFromFieldInfo(propName, value as FieldInfo);
        if (propType == typeof (MethodInfo) || value is MethodInfo)
            return GenerateXmlFromMethodInfo(propName, value as MethodInfo);
        if (propType == typeof (PropertyInfo) || value is PropertyInfo)
            return GenerateXmlFromPropertyInfo(propName, value as PropertyInfo);

        throw new NotSupportedException(propName);
    }

    private XElement GenerateXmlFromMethodInfo(string propName, MethodInfo methodInfo)
    {
        return methodInfo == null
                   ? new XElement(propName)
                   : new XElement(propName,
                                  new XAttribute("MemberType", methodInfo.MemberType),
                                  new XAttribute("MethodName", methodInfo.Name),
                                  GenerateXmlFromType("DeclaringType", methodInfo.DeclaringType),
                                  new XElement("Parameters",
                                               from param in methodInfo.GetParameters()
                                               select GenerateXmlFromType("Type", param.ParameterType)),
                                  new XElement("GenericArgTypes",
                                               from argType in methodInfo.GetGenericArguments()
                                               select GenerateXmlFromType("Type", argType)));
    }

    private XElement GenerateXmlFromPropertyInfo(string propName, PropertyInfo propertyInfo)
    {
        return propertyInfo == null
                   ? new XElement(propName)
                   : new XElement(propName,
                                  new XAttribute("MemberType", propertyInfo.MemberType),
                                  new XAttribute("PropertyName", propertyInfo.Name),
                                  GenerateXmlFromType("DeclaringType", propertyInfo.DeclaringType),
                                  new XElement("IndexParameters",
                                               from param in propertyInfo.GetIndexParameters()
                                               select GenerateXmlFromType("Type", param.ParameterType)));
    }

    private XElement GenerateXmlFromType(string propName, Type type)
    {
        return new XElement(propName, GenerateXmlFromTypeCore(type));
    }

    private XElement GenerateXmlFromElementInitList(string propName, IEnumerable<ElementInit> initializers)
    {
        if (initializers == null)
            initializers = new ElementInit[] {};
        return new XElement(propName,
                            from elementInit in initializers
                            select GenerateXmlFromElementInit(elementInit));
    }

    private XElement GenerateXmlFromExpressionList(string propName, IEnumerable<Expression> expressions)
    {
        return new XElement(propName,
                            from expression in expressions
                            select GenerateXmlFromExpressionCore(expression));
    }

    private XElement GenerateXmlFromMemberBindingList(string propName, IEnumerable<MemberBinding> bindings)
    {
        if (bindings == null)
            bindings = new MemberBinding[] {};
        return new XElement(propName,
                            from binding in bindings
                            select GenerateXmlFromMemberBinding(binding));
    }

    private XElement GenerateXmlFromMemberInfoList(string propName, IEnumerable<MemberInfo> members)
    {
        if (members == null)
            members = new MemberInfo[] {};
        return new XElement(propName,
                            from member in members
                            select GenerateXmlFromMemberInfo(member.GetType(), "Info", member));
    }

    private XElement GenerateXmlFromTypeCore(Type type)
    {
        // add detection of VB anon types
        if (type.Name.StartsWith("<>f__") || type.Name.StartsWith("VB$AnonymousType"))
            return new XElement("AnonymousType",
                                new XAttribute("Name", type.FullName ?? string.Empty),
                                from property in type.GetProperties()
                                select new XElement("Property",
                                                    new XAttribute("Name", property.Name),
                                                    GenerateXmlFromTypeCore(property.PropertyType)),
                                new XElement("Constructor",
                                             from parameter in type.GetConstructors().First().GetParameters()
                                             select new XElement("Parameter",
                                                                 new XAttribute("Name", parameter.Name),
                                                                 GenerateXmlFromTypeCore(parameter.ParameterType))
                                    ));
        // GetGenericArguments returns args for nongeneric types 
        if (type.IsGenericType)
        {
            return new XElement("Type",
                                new XAttribute("Name", type.GetGenericTypeDefinition().FullName ?? string.Empty),
                                from genArgType in type.GetGenericArguments()
                                select GenerateXmlFromTypeCore(genArgType));
        }
        return new XElement("Type", new XAttribute("Name", type.FullName ?? string.Empty));
    }

    private IEnumerable<T> AsIEnumerableOf<T>(object value)
    {
        return value == null ? null : (value as IEnumerable).Cast<T>();
    }

    #endregion Private Serializer Helper Method
}

/// <summary>
/// http://blogs.msdn.com/b/mattwar/archive/2007/08/01/linq-building-an-iqueryable-provider-part-iii.aspx
/// </summary>
internal static class Evaluator
{
    /// <summary>
    /// Performs evaluation & replacement of independent sub-trees
    /// </summary>
    /// <param name="expression">The root of the expression tree.</param>
    /// <param name="fnCanBeEvaluated">A function that decides whether a given expression node can be part of the local function.</param>
    /// <returns>A new tree with sub-trees evaluated and replaced.</returns>
    public static Expression PartialEval(Expression expression, Func<Expression, bool> fnCanBeEvaluated)
    {
        return new SubtreeEvaluator(new Nominator(fnCanBeEvaluated).Nominate(expression)).Eval(expression);
    }

    /// <summary>
    /// Performs evaluation & replacement of independent sub-trees
    /// </summary>
    /// <param name="expression">The root of the expression tree.</param>
    /// <returns>A new tree with sub-trees evaluated and replaced.</returns>
    public static Expression PartialEval(Expression expression)
    {
        return PartialEval(expression, Evaluator.CanBeEvaluatedLocally);
    }

    private static bool CanBeEvaluatedLocally(Expression expression)
    {
        if (expression.NodeType == ExpressionType.Constant &&
            typeof (IOrderedQueryable).IsAssignableFrom(expression.Type))
        {
            var elementType = ((IOrderedQueryable) ((ConstantExpression) expression).Value).ElementType;
            if (typeof (IOrderedQueryable<>).MakeGenericType(new[] {elementType}).IsAssignableFrom(expression.Type))
                return false;
        }
        return expression.NodeType != ExpressionType.Parameter;
    }

    /// <summary>
    /// Evaluates & replaces sub-trees when first candidate is reached (top-down)
    /// </summary>
    private class SubtreeEvaluator : ExpressionVisitor
    {
        private HashSet<Expression> candidates;

        internal SubtreeEvaluator(HashSet<Expression> candidates)
        {
            this.candidates = candidates;
        }

        internal Expression Eval(Expression exp)
        {
            return this.Visit(exp);
        }

        public override Expression Visit(Expression exp)
        {
            if (exp == null) return null;
            if (this.candidates.Contains(exp))
            {
                return this.Evaluate(exp);
            }
            return base.Visit(exp);
        }

        private Expression Evaluate(Expression e)
        {
            if (e.NodeType == ExpressionType.Constant) return e;

            LambdaExpression lambda = Expression.Lambda(e);
            Delegate fn = lambda.Compile();
            return Expression.Constant(fn.DynamicInvoke(null), e.Type);
        }
    }

    /// <summary>
    /// Performs bottom-up analysis to determine which nodes can possibly
    /// be part of an evaluated sub-tree.
    /// </summary>
    private class Nominator : ExpressionVisitor
    {
        private Func<Expression, bool> fnCanBeEvaluated;
        private HashSet<Expression> candidates;
        private bool cannotBeEvaluated;

        internal Nominator(Func<Expression, bool> fnCanBeEvaluated)
        {
            this.fnCanBeEvaluated = fnCanBeEvaluated;
        }

        internal HashSet<Expression> Nominate(Expression expression)
        {
            this.candidates = new HashSet<Expression>();
            this.Visit(expression);
            return this.candidates;
        }

        public override Expression Visit(Expression expression)
        {
            if (expression != null)
            {
                var saveCannotBeEvaluated = this.cannotBeEvaluated;
                this.cannotBeEvaluated = false;
                base.Visit(expression);
                if (!this.cannotBeEvaluated)
                {
                    if (this.fnCanBeEvaluated(expression))
                    {
                        this.candidates.Add(expression);
                    }
                    else
                    {
                        this.cannotBeEvaluated = true;
                    }
                }
                this.cannotBeEvaluated |= saveCannotBeEvaluated;
            }
            return expression;
        }
    }
}
<#+
}

void WriteDeserializer()
{
#>

internal class Deserializer
{
    #region Private Data Member

    private readonly Dictionary<string, ParameterExpression> _parameters =
        new Dictionary<string, ParameterExpression>();

    private readonly TypeResolver _resolver;

    #endregion Private Data Member

    #region Constructor

    public Deserializer(IEnumerable<Assembly> assemblies = null)
    {
        _resolver = new TypeResolver(assemblies);
    }

    #endregion Constructor

    #region Public Deserializer Method

    public Expression Deserialize(XElement xml)
    {
        _parameters.Clear();
        return ParseExpressionFromXmlNonNull(xml);
    }

    #endregion Public Deserializer Method

    #region Private Deserializer Method

    private Expression ParseExpressionFromXmlNonNull(XElement xml)
    {
        switch (xml.Name.LocalName)
        {
            case "BinaryExpression":
                return ParseBinaryExpressionFromXml(xml);
            case "ConditionalExpression":
                return ParseConditionalExpressionFromXml(xml);
            case "ConstantExpression":
                return ParseConstantExpressionFromXml(xml);
            case "InvocationExpression":
                return ParseInvocationExpressionFromXml(xml);
            case "LambdaExpression":
                return ParseLambdaExpressionFromXml(xml);
            case "ListInitExpression":
                return ParseListInitExpressionFromXml(xml);
            case "MemberExpression":
                return ParseMemberExpressionFromXml(xml);
            case "MemberInitExpression":
                return ParseMemberInitExpressionFromXml(xml);
            case "MethodCallExpression":
                return ParseMethodCallExpressionFromXml(xml);
            case "NewArrayExpression":
                return ParseNewArrayExpressionFromXml(xml);
            case "NewExpression":
                return ParseNewExpressionFromXml(xml);
            case "ParameterExpression":
                return ParseParameterExpressionFromXml(xml);
            case "TypeBinaryExpression":
                return ParseTypeBinaryExpressionFromXml(xml);
            case "UnaryExpression":
                return ParseUnaryExpressionFromXml(xml);
            default:
                throw new NotSupportedException(xml.Name.LocalName);
        }
    }

    /// <summary>
    /// Parse BinaryExpression From XElement 
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private BinaryExpression ParseBinaryExpressionFromXml(XElement xml)
    {
        var right = ParseExpressionFromXml(xml.Element("Right"));
        var left = ParseExpressionFromXml(xml.Element("Left"));
        var method = ParseMethodInfoFromXml(xml.Element("Method"));
        var conversion = ParseExpressionFromXml(xml.Element("Conversion")) as LambdaExpression;
        var isLiftedToNull = ParseWithDataContractSerializer<bool>(xml.Element("IsLiftedToNull"));
        var expressionType = ParseWithDataContractSerializer<ExpressionType>(xml.Element("NodeType"));
        return Expression.MakeBinary(expressionType, left, right, isLiftedToNull, method, conversion);
    }

    /// <summary>
    /// Parse ConditionalExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private ConditionalExpression ParseConditionalExpressionFromXml(XElement xml)
    {
        var test = ParseExpressionFromXml(xml.Element("Test"));
        var ifTrue = ParseExpressionFromXml(xml.Element("IfTrue"));
        var ifFalse = ParseExpressionFromXml(xml.Element("IfFalse"));
        var type = ParseTypeFromXml(xml.Element("Type"));
        return Expression.Condition(test, ifTrue, ifFalse, type);
    }

    /// <summary>
    /// Parse ConstantExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private ConstantExpression ParseConstantExpressionFromXml(XElement xml)
    {
        var type = ParseTypeFromXml(xml.Element("Type"));
        var result = ParseWithDataContractSerializer(xml.Element("Value"), type);
        return Expression.Constant(result, type);
    }

    /// <summary>
    /// Parse InvocationExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private InvocationExpression ParseInvocationExpressionFromXml(XElement xml)
    {
        var expression = ParseExpressionFromXml(xml.Element("Expression"));
        var arguments = ParseExpressionListFromXml<Expression>(xml, "Arguments");
        return Expression.Invoke(expression, arguments);
    }

    /// <summary>
    /// Parse LambdaExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private LambdaExpression ParseLambdaExpressionFromXml(XElement xml)
    {
        var type = ParseTypeFromXml(xml.Element("Type"));
        var body = ParseExpressionFromXml(xml.Element("Body"));
        var name = ParseWithDataContractSerializer<string>(xml.Element("Name"));
        var tailCall = ParseWithDataContractSerializer<bool>(xml.Element("TailCall"));
        var parameters = ParseExpressionListFromXml<ParameterExpression>(xml, "Parameters");
        return Expression.Lambda(type, body, name, tailCall, parameters);
    }

    /// <summary>
    /// Parse ListInitExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private ListInitExpression ParseListInitExpressionFromXml(XElement xml)
    {
        var newExpression = ParseExpressionFromXml(xml.Element("NewExpression")) as NewExpression;
        if (newExpression == null) throw new Exception("Expceted a NewExpression");
        var initializers = ParseElementInitListFromXml(xml, "Initializers");
        return Expression.ListInit(newExpression, initializers);
    }

    /// <summary>
    /// Parse MemberExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private MemberExpression ParseMemberExpressionFromXml(XElement xml)
    {
        var expression = ParseExpressionFromXml(xml.Element("Expression"));
        var member = ParseMemberInfoFromXml(xml.Element("Member"));
        return Expression.MakeMemberAccess(expression, member);
    }

    /// <summary>
    /// Parse MemberInitExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private MemberInitExpression ParseMemberInitExpressionFromXml(XElement xml)
    {
        var newExpression = ParseExpressionFromXml(xml.Element("NewExpression")) as NewExpression;
        if (newExpression == null) throw new Exception("Expceted a NewExpression");
        var bindings = ParseMemberBindingListFromXml(xml, "Bindings");
        if (bindings == null) throw new Exception("Bindings cannot be null");
        return Expression.MemberInit(newExpression, bindings);
    }

    /// <summary>
    /// Parse MethodCallExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private MethodCallExpression ParseMethodCallExpressionFromXml(XElement xml)
    {
        var instance = ParseExpressionFromXml(xml.Element("Object"));
        var method = ParseMethodInfoFromXml(xml.Element("Method"));
        var arguments = ParseExpressionListFromXml<Expression>(xml, "Arguments") ?? new Expression[0];
        return Expression.Call(instance, method, arguments);
    }

    /// <summary>
    /// Parse NewArrayExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private NewArrayExpression ParseNewArrayExpressionFromXml(XElement xml)
    {
        var type = ParseTypeFromXml(xml.Element("Type"));
        if (type == null) throw new Exception("Type cannot be null");
        if (!type.IsArray) throw new Exception("Expected array type");
        var elemType = type.GetElementType();
        var expressions = ParseExpressionListFromXml<Expression>(xml, "Expressions");
        var expressionType = ParseWithDataContractSerializer<ExpressionType>(xml.Element("NodeType"));
        switch (expressionType)
        {
            case ExpressionType.NewArrayInit:
                return Expression.NewArrayInit(elemType, expressions);
            case ExpressionType.NewArrayBounds:
                return Expression.NewArrayBounds(elemType, expressions);
            default:
                throw new Exception("Expected NewArrayInit or NewArrayBounds");
        }
    }

    /// <summary>
    /// Parse NewExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private NewExpression ParseNewExpressionFromXml(XElement xml)
    {
        var constructor = ParseConstructorInfoFromXml(xml.Element("Constructor"));
        var arguments = ParseExpressionListFromXml<Expression>(xml, "Arguments").ToArray();
        var members = ParseMemberInfoListFromXml<MemberInfo>(xml, "Members").ToArray();
        return members.Length == 0
                   ? Expression.New(constructor, arguments)
                   : Expression.New(constructor, arguments, members);
    }

    /// <summary>
    /// Parse ParameterExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private ParameterExpression ParseParameterExpressionFromXml(XElement xml)
    {
        var type = ParseTypeFromXml(xml.Element("Type"));
        var name = ParseWithDataContractSerializer<string>(xml.Element("Name"));
        var id = name + type.FullName;
        if (!_parameters.ContainsKey(id))
            _parameters.Add(id, Expression.Parameter(type, name));
        return _parameters[id];
    }

    /// <summary>
    /// Parse TypeBinaryExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private TypeBinaryExpression ParseTypeBinaryExpressionFromXml(XElement xml)
    {
        var expression = ParseExpressionFromXml(xml.Element("Expression"));
        var typeOperand = ParseTypeFromXml(xml.Element("TypeOperand"));
        var expressionType = ParseWithDataContractSerializer<ExpressionType>(xml.Element("NodeType"));
        switch (expressionType)
        {
            case ExpressionType.TypeEqual:
                return Expression.TypeEqual(expression, typeOperand);
            case ExpressionType.TypeIs:
                return Expression.TypeIs(expression, typeOperand);
            default:
                throw new Exception("Expected TypeEqual or TypeIs");
        }
    }

    /// <summary>
    /// Parse UnaryExpression From XElement
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private UnaryExpression ParseUnaryExpressionFromXml(XElement xml)
    {
        var operand = ParseExpressionFromXml(xml.Element("Operand"));
        var type = ParseTypeFromXml(xml.Element("Type"));
        var method = ParseMethodInfoFromXml(xml.Element("Method"));
        var expressionType = ParseWithDataContractSerializer<ExpressionType>(xml.Element("NodeType"));
        return Expression.MakeUnary(expressionType, operand, type, method);
    }

    #endregion Private Deserializer Method

    #region Private Deserializer Helper Method

    private ConstructorInfo ParseConstructorInfoFromXml(XElement xml)
    {
        if (xml.IsEmpty) return null;
        var declaringType = ParseTypeFromXml(xml.Element("DeclaringType"));
        var xElement = xml.Element("Parameters");
        if (xElement == null) throw new Exception("Parameters not found.");
        var parameters = from paramXml in xElement.Elements()
                         select ParseTypeFromXml(paramXml);
        return declaringType.GetConstructor(parameters.ToArray());
    }

    private ElementInit ParseElementInitFromXml(XElement xml)
    {
        var addMethod = ParseMethodInfoFromXml(xml.Element("AddMethod"));
        var arguments = ParseExpressionListFromXml<Expression>(xml, "Arguments");
        return Expression.ElementInit(addMethod, arguments);
    }

    private Expression ParseExpressionFromXml(XElement xml)
    {
        return xml.IsEmpty ? null : ParseExpressionFromXmlNonNull(xml.Elements().First());
    }

    private FieldInfo ParseFieldInfoFromXml(XElement xml)
    {
        var xAttribute = xml.Attribute("FieldName");
        if (xAttribute == null) throw new Exception("FieldName not found.");
        var fieldName = xAttribute.Value;
        var declaringType = ParseTypeFromXml(xml.Element("DeclaringType"));
        return declaringType.GetField(fieldName);
    }

    private MemberBinding ParseMemberBindingFromXml(XElement xml)
    {
        var member = ParseMemberInfoFromXml(xml.Element("Member"));
        switch (xml.Name.LocalName)
        {
            case "MemberAssignment":
                var expression = ParseExpressionFromXml(xml.Element("Expression"));
                return Expression.Bind(member, expression);
            case "MemberMemberBinding":
                var bindings = ParseMemberBindingListFromXml(xml, "Bindings");
                return Expression.MemberBind(member, bindings);
            case "MemberListBinding":
                var initializers = ParseElementInitListFromXml(xml, "Initializers");
                return Expression.ListBind(member, initializers);
        }
        throw new NotImplementedException();
    }

    private MemberInfo ParseMemberInfoFromXml(XElement xml)
    {
        var xAttribute = xml.Attribute("MemberType");
        if (xAttribute == null) throw new Exception("MemberType not found.");
        var memberType = (MemberTypes) Enum.Parse(typeof (MemberTypes), xAttribute.Value, false);
        switch (memberType)
        {
            case MemberTypes.Field:
                return ParseFieldInfoFromXml(xml);
            case MemberTypes.Property:
                return ParsePropertyInfoFromXml(xml);
            case MemberTypes.Method:
                return ParseMethodInfoFromXml(xml);
            case MemberTypes.Constructor:
                return ParseConstructorInfoFromXml(xml);
            default:
                throw new NotSupportedException(string.Format("MEmberType {0} not supported", memberType));
        }
    }

    private MethodInfo ParseMethodInfoFromXml(XElement xml)
    {
        if (xml.IsEmpty) return null;
        var xAttribute = xml.Attribute("MethodName");
        if (xAttribute == null) throw new Exception("MethodName not found.");
        var name = xAttribute.Value;
        var declaringType = ParseTypeFromXml(xml.Element("DeclaringType"));
        var xElement = xml.Element("Parameters");
        if (xElement == null) throw new Exception("Parameters not found.");
        var parameters = from paramXml in xElement.Elements()
                         select ParseTypeFromXml(paramXml);
        xElement = xml.Element("GenericArgTypes");
        if (xElement == null) throw new Exception("GenericArgTypes not found.");
        var genArgs = from argXml in xElement.Elements()
                      select ParseTypeFromXml(argXml);
        return _resolver.GetMethod(declaringType, name, parameters.ToArray(), genArgs.ToArray());
    }

    private PropertyInfo ParsePropertyInfoFromXml(XElement xml)
    {
        var xAttribute = xml.Attribute("PropertyName");
        if (xAttribute == null) throw new Exception("PropertyName not found.");
        var propertyName = xAttribute.Value;
        var declaringType = ParseTypeFromXml(xml.Element("DeclaringType"));
        return declaringType.GetProperty(propertyName);
    }

    private Type ParseTypeFromXml(XElement xml)
    {
        return ParseTypeFromXmlCore(xml.Elements().First());
    }

    private IEnumerable<ElementInit> ParseElementInitListFromXml(XElement xml, string elemName)
    {
        var xElement = xml.Element(elemName);
        if (xElement == null) throw new Exception(elemName + " not found.");
        return from tXml in xElement.Elements()
               select ParseElementInitFromXml(tXml);
    }

    private IEnumerable<T> ParseExpressionListFromXml<T>(XElement xml, string elemName) where T : Expression
    {
        var xElement = xml.Element(elemName);
        if (xElement == null) throw new Exception(elemName + " not found.");
        return from tXml in xElement.Elements()
               select (T) ParseExpressionFromXmlNonNull(tXml);
    }

    private IEnumerable<MemberBinding> ParseMemberBindingListFromXml(XElement xml, string elemName)
    {
        var xElement = xml.Element(elemName);
        if (xElement == null) throw new Exception(elemName + " not found.");
        return from tXml in xElement.Elements()
               select ParseMemberBindingFromXml(tXml);
    }

    private IEnumerable<T> ParseMemberInfoListFromXml<T>(XElement xml, string elemName) where T : MemberInfo
    {
        var xElement = xml.Element(elemName);
        if (xElement == null) throw new Exception(elemName + " not found.");
        return from tXml in xElement.Elements()
               select (T) ParseMemberInfoFromXml(tXml);
    }

    private Type ParseTypeFromXmlCore(XElement xml)
    {
        switch (xml.Name.ToString())
        {
            case "Type":
                return ParseNormalTypeFromXmlCore(xml);
            case "AnonymousType":
                return ParseAnonymousTypeFromXmlCore(xml);
            default:
                throw new ArgumentException("Expected 'Type' or 'AnonymousType'");
        }
    }

    private Type ParseNormalTypeFromXmlCore(XElement xml)
    {
        if (!xml.HasElements)
        {
            var xAttribute = xml.Attribute("Name");
            if (xAttribute == null) throw new Exception("Name not found.");
            return _resolver.GetType(xAttribute.Value);
        }

        var genericArgumentTypes = from genArgXml in xml.Elements()
                                   select ParseTypeFromXmlCore(genArgXml);
        var attribute = xml.Attribute("Name");
        if (attribute == null) throw new Exception("Name not found.");
        return _resolver.GetType(attribute.Value, genericArgumentTypes);
    }

    private Type ParseAnonymousTypeFromXmlCore(XElement xElement)
    {
        var xAttribute = xElement.Attribute("Name");
        if (xAttribute == null) throw new Exception("Name not found.");
        var name = xAttribute.Value;
        var properties = from propXml in xElement.Elements("Property")
                         let attribute = propXml.Attribute("Name")
                         where attribute != null
                         select new TypeResolver.NameTypePair
                             {
                                 Name = attribute.Value,
                                 Type = ParseTypeFromXml(propXml)
                             };
        var ctrParams = from propXml in xElement.Elements("Constructor").Elements("Parameter")
                        let attribute = propXml.Attribute("Name")
                        where attribute != null
                        select new TypeResolver.NameTypePair
                            {
                                Name = attribute.Value,
                                Type = ParseTypeFromXml(propXml)
                            };
        return _resolver.GetOrCreateAnonymousType(name, properties.ToArray(), ctrParams.ToArray());
    }

    private T ParseWithDataContractSerializer<T>(XElement xml)
    {
        if (xml == null) throw new Exception("XElement cannot be null.");
        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml.Value)))
        {
            var deserializer = new DataContractSerializer(typeof (T));
            return (T) deserializer.ReadObject(stream);
        }
    }

    private object ParseWithDataContractSerializer(XElement xml, Type expectedType)
    {
        if (xml == null) throw new Exception("XElement cannot be null.");
        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml.Value)))
        {
            var deserializer = new DataContractSerializer(expectedType);
            return deserializer.ReadObject(stream);
        }
    }

    #endregion Private Deserializer Helper Method
}
<#+
}

void WriteTypeResolver()
{
#>

internal sealed class TypeResolver
{
    #region Private Data Member

    private readonly Dictionary<AnonTypeId, Type> _anonymousTypes = new Dictionary<AnonTypeId, Type>();
    private readonly ModuleBuilder _moduleBuilder;
    private int _anonymousTypeIndex;

    private readonly HashSet<Assembly> _assemblies = new HashSet<Assembly>
        {
            typeof (String).Assembly,
            // mscorlib.dll
            typeof (ExpressionType).Assembly,
            // System.Core.dll
            typeof (XElement).Assembly,
            // System.Xml.Linq.dll
            Assembly.GetExecutingAssembly(),
        };

    #endregion Private Data Member

    #region Constructor

    public TypeResolver(IEnumerable<Assembly> assemblies = null)
    {
        var asmname = new AssemblyName {Name = "AnonymousTypes"};
        var assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(asmname,
                                                                       AssemblyBuilderAccess.Run);
        _moduleBuilder = assemblyBuilder.DefineDynamicModule("AnonymousTypes");

        if (assemblies != null)
        {
            foreach (var assembly in assemblies)
                _assemblies.Add(assembly);
        }
    }

    #endregion Constructor

    #region Public Method

    /// <summary>
    /// Get type of a generic type
    /// </summary>
    /// <param name="typeName"></param>
    /// <param name="genericArgumentTypes"></param>
    /// <returns></returns>
    public Type GetType(string typeName, IEnumerable<Type> genericArgumentTypes)
    {
        return GetType(typeName).MakeGenericType(genericArgumentTypes.ToArray());
    }

    /// <summary>
    /// Get type based on type name
    /// </summary>
    /// <param name="typeName"></param>
    /// <returns></returns>
    public Type GetType(string typeName)
    {
        Type type;
        if (string.IsNullOrEmpty(typeName))
            throw new ArgumentNullException("typeName");

        // array type
        if (typeName.EndsWith("[]"))
            return GetType(typeName.Substring(0, typeName.Length - 2)).MakeArrayType();

        // load type from assemblies
        foreach (var assembly in _assemblies)
        {
            type = assembly.GetType(typeName);
            if (type != null)
                return type;
        }

        // call Type.GetType()
        type = Type.GetType(typeName, false, true);
        if (type != null)
            return type;

        throw new ArgumentException("Could not find a matching type", typeName);
    }

    /// <summary>
    /// Get method based on type, name and parameters
    /// </summary>
    /// <param name="declaringType"></param>
    /// <param name="name"></param>
    /// <param name="parameterTypes"></param>
    /// <param name="genArgTypes"></param>
    /// <returns></returns>
    public MethodInfo GetMethod(Type declaringType, string name, Type[] parameterTypes, Type[] genArgTypes)
    {
        var methods = from mi in declaringType.GetMethods()
                      where mi.Name == name
                      select mi;
        foreach (var method in methods)
        {
            try
            {
                var realMethod = method;
                if (method.IsGenericMethod)
                {
                    realMethod = method.MakeGenericMethod(genArgTypes);
                }
                var methodParameterTypes = realMethod.GetParameters().Select(p => p.ParameterType);
                if (MatchPiecewise(parameterTypes, methodParameterTypes))
                {
                    return realMethod;
                }
            }
            catch (ArgumentException)
            {
            }
        }
        return null;
    }

    /// <summary>
    /// Get or create anonymous type
    /// </summary>
    /// <param name="name"></param>
    /// <param name="properties"></param>
    /// <param name="ctrParams"></param>
    /// <returns></returns>
    public Type GetOrCreateAnonymousType(string name, NameTypePair[] properties, NameTypePair[] ctrParams)
    {
        var id = new AnonTypeId(name, properties.Concat(ctrParams));
        if (_anonymousTypes.ContainsKey(id))
            return _anonymousTypes[id];

        const string anonPrefix = "<>f__AnonymousType";
        var anonTypeBuilder = _moduleBuilder.DefineType(anonPrefix + _anonymousTypeIndex++,
                                                        TypeAttributes.Public | TypeAttributes.Class);

        var fieldBuilders = new FieldBuilder[properties.Length];
        var propertyBuilders = new PropertyBuilder[properties.Length];

        for (var i = 0; i < properties.Length; i++)
        {
            fieldBuilders[i] = anonTypeBuilder.DefineField("_generatedfield_" + properties[i].Name,
                                                           properties[i].Type, FieldAttributes.Private);
            propertyBuilders[i] = anonTypeBuilder.DefineProperty(properties[i].Name,
                                                                 System.Reflection.PropertyAttributes.None,
                                                                 properties[i].Type, new Type[0]);
            var propertyGetterBuilder = anonTypeBuilder.DefineMethod("get_" + properties[i].Name,
                                                                     MethodAttributes.Public,
                                                                     properties[i].Type, new Type[0]);
            var getIlGenerator = propertyGetterBuilder.GetILGenerator();
            getIlGenerator.Emit(OpCodes.Ldarg_0);
            getIlGenerator.Emit(OpCodes.Ldfld, fieldBuilders[i]);
            getIlGenerator.Emit(OpCodes.Ret);
            propertyBuilders[i].SetGetMethod(propertyGetterBuilder);
        }

        var constructorBuilder = anonTypeBuilder.DefineConstructor(
            MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Public,
            CallingConventions.Standard, ctrParams.Select(prop => prop.Type).ToArray());
        var constructorIlGenerator = constructorBuilder.GetILGenerator();
        for (var i = 0; i < ctrParams.Length; i++)
        {
            constructorIlGenerator.Emit(OpCodes.Ldarg_0);
            constructorIlGenerator.Emit(OpCodes.Ldarg, i + 1);
            constructorIlGenerator.Emit(OpCodes.Stfld, fieldBuilders[i]);
            constructorBuilder.DefineParameter(i + 1, ParameterAttributes.None, ctrParams[i].Name);
        }
        constructorIlGenerator.Emit(OpCodes.Ret);

        var anonType = anonTypeBuilder.CreateType();
        _anonymousTypes.Add(id, anonType);
        return anonType;
    }

    #endregion Public Method

    #region Private Method

    private bool MatchPiecewise<T>(IEnumerable<T> first, IEnumerable<T> second)
    {
        var firstArray = first.ToArray();
        var secondArray = second.ToArray();
        if (firstArray.Length != secondArray.Length)
            return false;
        return !firstArray.Where((t, i) => !t.Equals(secondArray[i])).Any();
    }

    #endregion Private Method

    #region nested classes

    public class NameTypePair
    {
        public string Name { get; set; }
        public Type Type { get; set; }

        public override int GetHashCode()
        {
            return Name.GetHashCode() + Type.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (!(obj is NameTypePair))
                return false;
            var other = obj as NameTypePair;
            return Name.Equals(other.Name) && Type.Equals(other.Type);
        }
    }

    private class AnonTypeId
    {
        public string Name { get; private set; }
        public IEnumerable<NameTypePair> Properties { get; private set; }

        public AnonTypeId(string name, IEnumerable<NameTypePair> properties)
        {
            Name = name;
            Properties = properties;
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode() + Properties.Sum(ntpair => ntpair.GetHashCode());
        }

        public override bool Equals(object obj)
        {
            if (!(obj is AnonTypeId))
                return false;
            var other = obj as AnonTypeId;
            return (Name.Equals(other.Name)
                    && Properties.SequenceEqual(other.Properties));
        }
    }

    #endregion
}
<#+
}

private void DefineMetadata()
{
    TemplateMetadata[MetadataConstants.TT_TEMPLATE_NAME] = "CSharpSelfTracking.Types";
    TemplateMetadata[MetadataConstants.TT_TEMPLATE_VERSION] = "5.0";
}
#>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions