//-------------------------------------------------------------------------------------------------------------------------------------------------
            protected override void OnImplementBaseClass(ImplementationClassWriter<TT.TBase> writer)
            {
                var modelTypeKey = (EdmModelTypeKey)base.Context.TypeKey;
                var model = modelTypeKey.Model;

                writer.Constructor<Uri>((w, uri) => w.Base<Uri, string>(uri, w.Const(model.GetEntityNamespace())));

                var allEntityTypes = model.SchemaElements.OfType<IEdmEntityType>().ToArray();

                foreach ( var entityEdmType in allEntityTypes )
                {
                    var entityClrType = _entityFactory.ImplementClientEntity(model, entityEdmType);

                    using ( TT.CreateScope<TT.TItem>(entityClrType) )
                    {
                        writer.NewVirtualWritableProperty<DataServiceQuery<TT.TItem>>(entityEdmType.Name).Implement(
                            p => p.Get(w => {
                                w.If(p.BackingField == w.Const<DataServiceQuery<TT.TItem>>(null)).Then(() =>
                                    p.BackingField.Assign(
                                        w.This<DataServiceContext>().Func<string, DataServiceQuery<TT.TItem>>(
                                            x => x.CreateQuery<TT.TItem>, w.Const(entityEdmType.Name)))
                                );
                                w.Return(p.BackingField);
                            }),
                            p => p.Set((w, value) => { }));
                    }
                }
            }
            //-----------------------------------------------------------------------------------------------------------------------------------------------------
            private void WriteEntityProperty(
                ImplementationClassWriter<TypeTemplate.TBase> writer, 
                IEdmModel model,
                IEdmProperty property, 
                List<Action<ConstructorWriter>> initializers)
            {
                var propertyClrType = TranslateEdmTypeToClrType(model, property.Type.Definition);

                using (TT.CreateScope<TT.TProperty>(propertyClrType))
                {
                    var backingField = writer.Field<TT.TProperty>("_" + property.Name);

                    if (property.Type.IsCollection())
                    {
                        Type elementClrType = propertyClrType.GetGenericArguments()[0];

                        initializers.Add(cw => {
                            using (TT.CreateScope<TT.TProperty, TT.TItem>(propertyClrType, elementClrType))
                            {
                                backingField.Assign(cw.New<TT.TProperty>(cw.Const<IEnumerable<TT.TItem>>(null), cw.Const(TrackingMode.None)));
                            }
                        });
                    }

                    writer.NewVirtualWritableProperty<TT.TProperty>(property.Name).ImplementAutomatic(backingField);
                }
            }