Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

, 20 Feb 2012 CPOL
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.
SchoolSample_20111201.zip
SchoolSample
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
Assemblies
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
ComponentModel.Composition.Initialization.Desktop
Microsoft
ComponentModel
Composition
Hosting
Internal
Properties
System
ComponentModel
Composition
Hosting
System.Windows.Controls.Data.Input
Common
Properties
Settings.settings
themes
Validation
Database
SchoolSample
SchoolSample.csproj.user
Asset
Control
Properties
Settings.settings
View
SchoolSample.Common
Model
Properties
Resource
SchoolSample.Data
EntityModel
Properties
Validation
SchoolSample.Data.Wcf
EntityModel
SchoolModel.edmx
Properties
Validation
SchoolSample.Model
Properties
SchoolSample.ViewModel
Properties
SchoolSample.Wcf
SchoolSample.Wcf.csproj.user
Properties
Service
SchoolService.svc
SchoolSample.WCFService
SchoolSample.WCFService.csproj.user
Properties
Service References
SchoolService
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
Test.SchoolSample.Model
Properties
Test References
SchoolSample.Model.accessor
Test.SchoolSample.ViewModel
Properties
Test References
SchoolSample.ViewModel.accessor
SchoolSample_20120117.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
SchoolSample_20120216.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#

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";
string codeForIEditableObject = @"true";

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

WriteHeader(fileManager);
#>
#if (SILVERLIGHT || WPF)
<#
BeginNamespace(namespaceName, code);
WriteNotifyValidationResultChanged();
WriteINodeLevelInterface();
WriteClientChangeTracking();
EndNamespace(namespaceName);
#>
#endif
<#

// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().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.Escape(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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // estimate size of all Primitive Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity))
        {
            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;
            }
        }
        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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Primitive Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Primitive Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
                    if (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.Binary &&
                        entity.NavigationProperties.Where(np=>np.GetDependentProperties().Contains(edmProperty)).Any())
                    {
#>
        if (!EqualityComparer.BinaryEquals(<#=code.Escape(edmProperty)#>, (<#=code.Escape(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"]))
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(edmProperty)#> != (<#=code.Escape(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"])
<#
                    }
#>
            <#=code.Escape(edmProperty)#> = (<#=code.Escape(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.Escape(navProperty.ToEndMember.GetEntityType())#>>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
                else
                {
#>
        <#=code.Escape(navProperty)#> = (<#=code.Escape(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")
    {
        fileManager.StartNewFile("WPF4." + entity.Name + ".cs");
#>
#if WPF
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#><#=entity.BaseType == null ? " : " : ", "#>IClientChangeTracking, IChangeTracking, IRevertibleChangeTracking,
                             IDataErrorInfo, 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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
        {
#>
            // estimate size of all Primitive Properties
<#
        }
        foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity))
        {
            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;
            }
        }
        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("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();
        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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Primitive Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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 PrimitiveType && p.DeclaringType == entity).Count() > 0)
            {
#>
        // copy all Primitive Properties except the primary key fields
<#
            }
            foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity))
            {
                if (!ef.IsKey(edmProperty))
                {
                    if (((PrimitiveType)edmProperty.TypeUsage.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.Binary &&
                        entity.NavigationProperties.Where(np=>np.GetDependentProperties().Contains(edmProperty)).Any())
                    {
#>
        if (!EqualityComparer.BinaryEquals(<#=code.Escape(edmProperty)#>, (<#=code.Escape(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"]))
<#
                    }
                    else
                    {
#>
        if (<#=code.Escape(edmProperty)#> != (<#=code.Escape(edmProperty.TypeUsage)#>)_cache["<#=code.Escape(edmProperty)#>"])
<#
                    }
#>
            <#=code.Escape(edmProperty)#> = (<#=code.Escape(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.Escape(navProperty.ToEndMember.GetEntityType())#>>)_cache["<#=code.Escape(navProperty)#>"];
<#
                }
                else
                {
#>
        <#=code.Escape(navProperty)#> = (<#=code.Escape(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
<#
    }
}

// Emit Complex Types
foreach (ComplexType complex in ItemCollection.GetItems<ComplexType>().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.Escape(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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // estimate size of all Primitive Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex))
        {
            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;
            }
        }
        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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
            {
#>
            // copy all Primitive Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // copy all Primitive Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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")
    {
        fileManager.StartNewFile("WPF4." + complex.Name + ".cs");
#>
#if WPF
<#
        BeginNamespace(namespaceName, code);
#>
<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#><#=code.StringBefore(" : ", code.Escape(complex.BaseType))#><#=complex.BaseType == null ? " : " : ", "#>IDataErrorInfo, INotifyValidationResultChanged<#=codeForIEditableObject == "true" ? ", IEditableObject" : "" #>
{
<#
        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();
        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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
        {
#>
            // estimate size of all Primitive Properties
<#
        }
        foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex))
        {
            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;
            }
        }
        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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
            {
#>
            // copy all Primitive Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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 PrimitiveType && p.DeclaringType == complex).Count() > 0)
            {
#>
        // copy all Primitive Properties
<#
            }
            foreach (EdmProperty edmProperty in complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && 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 (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))
{
    return "";
}

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.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.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 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 ofa 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;
<#+
}

bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
{
    Dictionary<string, bool> alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
    foreach (StructuralType type in itemCollection.GetItems<StructuralType>())
    {
        if (!(type is EntityType || type is ComplexType))
        {
            continue;
        }

        if (alreadySeen.ContainsKey(type.FullName))
        {
            Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
            return false;
        }
        else
        {
            alreadySeen.Add(type.FullName, true);
        }
    }

    return true;
}
#>

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)

Share

About the Author

Weidong Shen
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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 20 Feb 2012
Article Copyright 2011 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid