예제 #1
0
        public Task <bool> SaveAsync(IUpdateEntry entry, CancellationToken cancellationToken = default)
        {
            var id = entry.GetCurrentValue <string>(_entityType.FindProperty(StoreKeyConvention.IdPropertyName));

            switch (entry.EntityState)
            {
            case EntityState.Added:
                return(_cosmosClient.CreateDocumentAsync(_collectionId, CreateDocument(entry), cancellationToken));

            case EntityState.Modified:
                var jObjectProperty = _entityType.FindProperty(StoreKeyConvention.JObjectPropertyName);
                var document        = jObjectProperty != null ? (JObject)entry.GetCurrentValue(jObjectProperty) : null;
                if (document != null)
                {
                    UpdateDocument(document, entry);
                }
                else
                {
                    document = CreateDocument(entry);

                    // Set Discriminator Property for updates
                    document[_entityType.CosmosSql().DiscriminatorProperty.Name] =
                        JToken.FromObject(_entityType.CosmosSql().DiscriminatorValue);
                }

                return(_cosmosClient.ReplaceDocumentAsync(_collectionId, id, document, cancellationToken));

            case EntityState.Deleted:
                return(_cosmosClient.DeleteDocumentAsync(_collectionId, id, cancellationToken));
            }

            return(Task.FromResult(false));
        }