public Task SaveAsync(IUpdateEntry entry, CancellationToken cancellationToken = default)
        {
            var id = GetId(entry);

            switch (entry.EntityState)
            {
            case EntityState.Added:
                return(_documentClient.CreateDocumentAsync(
                           UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionName),
                           CreateDocument(id, entry)));

            case EntityState.Modified:
                var document = CreateDocument(id, entry);

                // Set Discriminator Property for updates
                document.SetPropertyValue(
                    _entityType.CosmosSql().DiscriminatorProperty.Name,
                    _entityType.CosmosSql().DiscriminatorValue);

                return(_documentClient.ReplaceDocumentAsync(
                           UriFactory.CreateDocumentUri(_databaseId, _collectionName, id),
                           document));

            case EntityState.Deleted:
                return(_documentClient.DeleteDocumentAsync(
                           UriFactory.CreateDocumentUri(_databaseId, _collectionName, id)));
            }

            return(Task.CompletedTask);
        }
Пример #2
0
        public bool Save(IUpdateEntry entry)
        {
            var id = entry.GetCurrentValue <string>(_entityType.FindProperty(StoreKeyConvention.IdPropertyName));

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

            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.ReplaceDocument(_collectionId, id, document));

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

            return(false);
        }
        public BinaryExpression GetDiscriminatorPredicate(IEntityType entityType)
        {
            if (!EntityType.IsAssignableFrom(entityType))
            {
                return(null);
            }

            var discriminatorProperty = entityType.CosmosSql().DiscriminatorProperty;

            return(MakeBinary(
                       ExpressionType.Equal,
                       new KeyAccessExpression(discriminatorProperty, FromExpression),
                       Constant(entityType.CosmosSql().DiscriminatorValue, discriminatorProperty.ClrType)));
        }
 public DocumentCollectionUpdater(
     CosmosClient cosmosClient,
     IEntityType entityType)
 {
     _cosmosClient = cosmosClient;
     _collectionId = entityType.CosmosSql().CollectionName;
     _entityType   = entityType;
 }
        public SelectExpression(IEntityType entityType, IQuerySource querySource)
        {
            Projection = new EntityProjectionExpression(entityType, _rootAlias);

            FromExpression = new RootReferenceExpression(entityType, _rootAlias);

            // Add discriminator predicate
            var discriminatorProperty = entityType.CosmosSql().DiscriminatorProperty;

            FilterExpression = MakeBinary(
                ExpressionType.Equal,
                new KeyAccessExpression(discriminatorProperty, FromExpression),
                Constant(entityType.CosmosSql().DiscriminatorValue, discriminatorProperty.ClrType));

            EntityType   = entityType;
            _querySource = querySource;
        }
 public DocumentCollectionUpdater(
     CosmosClient cosmosClient,
     IEntityType entityType)
 {
     _documentClient = cosmosClient.DocumentClient;
     _databaseId     = cosmosClient.DatabaseId;
     _collectionName = entityType.CosmosSql().CollectionName;
     _entityType     = entityType;
 }
 public DocumentCollection(
     CosmosClient cosmosClient,
     IEntityType entityType,
     IPrincipalKeyValueFactory <TKey> principalKeyValueFactory)
 {
     _documentClient           = cosmosClient.DocumentClient;
     _databaseId               = cosmosClient.DatabaseId;
     _collectionName           = entityType.CosmosSql().CollectionName;
     _entityType               = entityType;
     _principalKeyValueFactory = principalKeyValueFactory;
 }
        public bool Save(IUpdateEntry entry)
        {
            var id = entry.GetCurrentValue <string>(_entityType.FindProperty("id"));

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

            case EntityState.Modified:
                var document = CreateDocument(entry);

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

                return(_cosmosClient.ReplaceDocument(_collectionId, id, document));

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

            return(false);
        }