Esempio n. 1
0
        private void DeleteLink(TKey key, string navigationProperty, object linkedObject)
        {
            IEdmNavigationProperty edmNavigationProperty = GenericNavigationPropertyRoutingConvention.GetNavigationProperty(Request.ODataProperties().Path);

            Contract.Assert(navigationProperty == edmNavigationProperty.Name);

            // Find the containing entity
            TEntity entity = DbSet.Find(key);

            if (entity == null)
            {
                string error = string.Format("Entity lookup failed for key {0} in {1}", key, DbSet);
                throw new ArgumentException(error, "key");
            }

            if (edmNavigationProperty.Type.IsCollection())
            {
                if (linkedObject == null)
                {
                    throw new ArgumentNullException("linkedObject", "A valid object to be deleted must be provided to remove from a collection property.");
                }
                object propertyCollection = entity.GetPropertyValue(navigationProperty);
                propertyCollection.InvokeMethod("Remove", linkedObject);
            }
            else
            {
                entity.SetPropertyValue(navigationProperty, null);
            }

            this.OnChangeSetSuccess(SaveChanges);
        }
Esempio n. 2
0
        public override CreatedItemResult <TProperty> PostNavigationProperty <TProperty>([ModelBinder(typeof(ChangeSetEntityModelBinder))] TEntity entity,
                                                                                         string navigationProperty,
                                                                                         TProperty propertyEntity)
        {
            IEdmNavigationProperty edmNavigationProperty = GenericNavigationPropertyRoutingConvention.GetNavigationProperty(Request.ODataProperties().Path);

            Contract.Assert(navigationProperty == edmNavigationProperty.Name);

            // Add the new propertyEntity to the appropriate DbSet; Find its EntitySet first
            //IEdmEntityType edmEntityType = edmNavigationProperty.ToEntityType();
            //IEntitySetMetadata entitySetMetadata = ContainerMetadata.GetEntitySetFor(edmEntityType);
            //if (entitySetMetadata == null)
            //{
            //	throw new InvalidOperationException("Unable to find the entityset for entity type " + edmEntityType.ToTraceString());
            //}
            //Db.AddEntity(entitySetMetadata.Name, propertyEntity);

            if (edmNavigationProperty.Type.IsCollection())
            {
                object propertyCollection = entity.GetPropertyValue(navigationProperty);
                propertyCollection.InvokeMethod("Add", propertyEntity);
            }
            else
            {
                entity.SetPropertyValue(navigationProperty, propertyEntity);
            }

            this.OnChangeSetSuccess(SaveChanges);

            return(Created(propertyEntity));
        }
Esempio n. 3
0
        public override void CreateLink([ModelBinder(typeof(ChangeSetEntityModelBinder))] TEntity entity, string navigationProperty, [FromBody] Uri link)
        {
            IEdmNavigationProperty edmNavigationProperty = GenericNavigationPropertyRoutingConvention.GetNavigationProperty(Request.ODataProperties().Path);

            Contract.Assert(navigationProperty == edmNavigationProperty.Name);

            // Fetch the linked object either via a ChangeSet/Content-ID reference, or by fetching it from the database.
            object linkedObject = null;

            if (!Request.ContentIdReferenceToEntity(link.OriginalString, out linkedObject))
            {
                linkedObject = GetEntityForLink(link);
            }
            if (linkedObject == null)
            {
                throw new ArgumentException(string.Format("Link: {0} could not be resolved to an entity", link), "link");
            }

            if (edmNavigationProperty.Type.IsCollection())
            {
                object propertyCollection = entity.GetPropertyValue(navigationProperty);
                propertyCollection.InvokeMethod("Add", linkedObject);
            }
            else
            {
                entity.SetPropertyValue(navigationProperty, linkedObject);
            }

            this.OnChangeSetSuccess(SaveChanges);
        }
Esempio n. 4
0
        /// <summary>
        /// Handles GET requests on navigation properties.
        /// </summary>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="key"></param>
        /// <param name="navigationProperty"></param>
        /// <param name="queryOptions"></param>
        /// <returns></returns>
        public virtual HttpResponseMessage GetNavigationProperty<TProperty>([FromODataUri] TKey key, string navigationProperty, ODataQueryOptions<TProperty> queryOptions)
            where TProperty : class
        {
            Contract.Requires<ArgumentException>(! string.IsNullOrWhiteSpace(navigationProperty));
            Contract.Requires<ArgumentNullException>(queryOptions != null);

            queryOptions.Validate(QueryValidationSettings);

            IEdmNavigationProperty edmNavigationProperty = GenericNavigationPropertyRoutingConvention.GetNavigationProperty(Request.ODataProperties().Path);
            Contract.Assert(navigationProperty == edmNavigationProperty.Name);

            IQueryable<TEntity> query = GetEntityWithNavigationPropertyQuery<TProperty>(key, edmNavigationProperty);

            // TODO: Make this work to support $expand on GET navigation property
            //IQueryable<TProperty> query = GetKeyBasedNavigationPropertyQuery(key, navigationProperty, edmNavigationProperty);
            //IQueryable queryOptionsApplied = queryOptions.ApplyTo(query);

            return Request.CreateSingleEntityProjectedResponse(query, entity => entity.GetPropertyValue(navigationProperty));
        }
Esempio n. 5
0
        public override void DeleteLink([FromODataUri] TKey key, string relatedKey, string navigationProperty)
        {
            IEdmNavigationProperty edmNavigationProperty = GenericNavigationPropertyRoutingConvention.GetNavigationProperty(Request.ODataProperties().Path);

            Contract.Assert(navigationProperty == edmNavigationProperty.Name);

            // Get the DbSet containing the linked object
            IEdmSchemaType schemaType  = ((Microsoft.Data.Edm.Library.EdmNavigationProperty)edmNavigationProperty.Partner).DeclaringEntityType;
            Type           relatedType = ContainerMetadata.GetEntitySetFor(schemaType).ElementTypeMetadata.ClrType;
            object         linkKey     = ODataUriUtils.ConvertFromUriLiteral(relatedKey,
                                                                             ODataVersion.V3,
                                                                             Request.ODataProperties().Model,
                                                                             Request.ODataProperties().Path.EntitySet.GetSingleKeyType());
            object linkedObject = Db.Set(relatedType).Find(linkKey);

            if (linkedObject == null)
            {
                throw new ArgumentException(string.Format("Link: {0}.{1} could not be resolved to an entity", navigationProperty, relatedKey), "link");
            }

            DeleteLink(key, navigationProperty, linkedObject);
        }