Click here to Skip to main content
15,886,422 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();
#>
}
<#
    WriteSerializer();
}
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.Resources;
using System.Runtime.Serialization;
using System.Text;
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> 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);
}
<#+
}

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;

    #endregion Private Data Member

    #region Constructor

    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> 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));
    }

    #endregion Interface IClientQuery<T> Implementation

    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
    }
}
<#+
}

void WriteSerializer()
{
#>

internal class Serializer
{
    #region Public Serialize Method

    public XElement Serialize(Expression e)
    {
        e = new ExpressionEvaluator().Evaluate(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
}

internal sealed class ExpressionEvaluator : ExpressionVisitor
{
    #region Private Data Member

    private HashSet<Expression> _subTrees = new HashSet<Expression>();
    private bool _initializeSubTrees;
    private bool _hasParameter;

    #endregion Private Data Member

    #region Public Method

    public Expression Evaluate(Expression expression)
    {
        _subTrees = new HashSet<Expression>();
        _initializeSubTrees = true;
        Visit(expression);
        _initializeSubTrees = false;
        return Visit(expression);
    }

    #endregion Public Method

    #region Visit Override

    public override Expression Visit(Expression expression)
    {
        return _initializeSubTrees ? InitializeSubTrees(expression) : VisitSubTrees(expression);
    }

    private Expression InitializeSubTrees(Expression expression)
    {
        if (expression != null)
        {
            var hasParameter = _hasParameter;
            _hasParameter = false;
            base.Visit(expression);

            if (!_hasParameter)
            {
                if (expression.NodeType != ExpressionType.Parameter)
                {
                    if (expression.NodeType != ExpressionType.Constant &&
                        expression.NodeType != ExpressionType.Lambda)
                    {
                        _subTrees.Add(expression);
                    }
                }
                else
                    _hasParameter = true;
            }
            _hasParameter |= hasParameter;
        }
        return expression;
    }

    private Expression VisitSubTrees(Expression expression)
    {
        if (expression == null)
        {
            return null;
        }
        if (_subTrees.Contains(expression))
        {
            var lambda = Expression.Lambda(expression);
            var delegateFunction = lambda.Compile();
            var result = delegateFunction.DynamicInvoke(null);
            return Expression.Constant(result, expression.Type);
        }
        return base.Visit(expression);
    }

    #endregion Visit Override
}
<#+
}

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