protected internal void AddEntity(Entity e)
        {
            //Automatically detect proxy types assembly if an early bound type was used.
            if (ProxyTypesAssembly == null &&
                e.GetType().IsSubclassOf(typeof(Entity)))
            {
                ProxyTypesAssembly = Assembly.GetAssembly(e.GetType());
            }

            ValidateEntity(e); //Entity must have a logical name and an Id

            //Add the entity collection
            if (!Data.ContainsKey(e.LogicalName))
            {
                Data.Add(e.LogicalName, new Dictionary <Guid, Entity>());
            }

            if (Data[e.LogicalName].ContainsKey(e.Id))
            {
                Data[e.LogicalName][e.Id] = e;
            }
            else
            {
                Data[e.LogicalName].Add(e.Id, e);
            }


            //Update metadata for that entity
            if (!AttributeMetadata.ContainsKey(e.LogicalName))
            {
                AttributeMetadata.Add(e.LogicalName, new Dictionary <string, string>());
            }

            //Update attribute metadata
            if (ProxyTypesAssembly != null)
            {
                //If the context is using a proxy types assembly then we can just guess the metadata from the generated attributes
                var type = FindReflectedType(e.LogicalName);
                if (type != null)
                {
                    var props = type.GetProperties();
                    foreach (var p in props)
                    {
                        if (!AttributeMetadata[e.LogicalName].ContainsKey(p.Name))
                        {
                            AttributeMetadata[e.LogicalName].Add(p.Name, p.Name);
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format("Couldnt find reflected type for {0}", e.LogicalName));
                }
            }
            else
            {
                //If dynamic entities are being used, then the only way of guessing if a property exists is just by checking
                //if the entity has the attribute in the dictionary
                foreach (var attKey in e.Attributes.Keys)
                {
                    if (!AttributeMetadata[e.LogicalName].ContainsKey(attKey))
                    {
                        AttributeMetadata[e.LogicalName].Add(attKey, attKey);
                    }
                }
            }
        }