private bool Save(IUpdateEntry entry)
        {
            var entityType     = entry.EntityType;
            var documentSource = GetDocumentSource(entityType);
            var collectionId   = documentSource.GetCollectionId();
            var state          = entry.EntityState;

            if (entry.SharedIdentityEntry != null)
            {
                if (entry.EntityState == EntityState.Deleted)
                {
                    return(false);
                }

                if (state == EntityState.Added)
                {
                    state = EntityState.Modified;
                }
            }

            switch (state)
            {
            case EntityState.Added:
                var newDocument = documentSource.CreateDocument(entry);
                newDocument["__partitionKey"] = "0";
                return(_cosmosClient.CreateItem(collectionId, newDocument));

            case EntityState.Modified:
                var jObjectProperty = entityType.FindProperty(StoreKeyConvention.JObjectPropertyName);
                var document        = jObjectProperty != null
                        ? (JObject)(entry.SharedIdentityEntry ?? entry).GetCurrentValue(jObjectProperty)
                        : null;
                if (document != null)
                {
                    if (documentSource.UpdateDocument(document, entry) == null)
                    {
                        return(false);
                    }
                }
                else
                {
                    document = documentSource.CreateDocument(entry);
                    document["__partitionKey"] = "0";

                    document[entityType.GetDiscriminatorProperty().GetCosmosPropertyName()] =
                        JToken.FromObject(entityType.GetDiscriminatorValue(), CosmosClientWrapper.Serializer);
                }

                return(_cosmosClient.ReplaceItem(
                           collectionId, documentSource.GetId(entry.SharedIdentityEntry ?? entry), document));

            case EntityState.Deleted:
                return(_cosmosClient.DeleteItem(collectionId, documentSource.GetId(entry)));

            default:
                return(false);
            }
        }
        private bool Save(IUpdateEntry entry)
        {
            var entityType     = entry.EntityType;
            var documentSource = GetDocumentSource(entityType);
            var collectionId   = documentSource.GetCollectionId();
            var state          = entry.EntityState;

            if (entry.SharedIdentityEntry != null)
            {
                if (entry.EntityState == EntityState.Deleted)
                {
                    return(false);
                }

                if (state == EntityState.Added)
                {
                    state = EntityState.Modified;
                }
            }

            switch (state)
            {
            case EntityState.Added:
                var newDocument = documentSource.CreateDocument(entry);

                return(_cosmosClient.CreateItem(collectionId, newDocument, GetPartitionKey(entry)));

            case EntityState.Modified:
                var document = documentSource.GetCurrentDocument(entry);
                if (document != null)
                {
                    if (documentSource.UpdateDocument(document, entry) == null)
                    {
                        return(false);
                    }
                }
                else
                {
                    document = documentSource.CreateDocument(entry);

                    var propertyName = entityType.GetDiscriminatorProperty()?.GetPropertyName();
                    if (propertyName != null)
                    {
                        document[propertyName] =
                            JToken.FromObject(entityType.GetDiscriminatorValue(), CosmosClientWrapper.Serializer);
                    }
                }

                return(_cosmosClient.ReplaceItem(
                           collectionId, documentSource.GetId(entry.SharedIdentityEntry ?? entry), document, GetPartitionKey(entry)));

            case EntityState.Deleted:
                return(_cosmosClient.DeleteItem(collectionId, documentSource.GetId(entry), GetPartitionKey(entry)));

            default:
                return(false);
            }
        }