Esempio n. 1
0
            public override void RemoveEntity(Object dataContext, ODataResourceBase entry)
            {
                var context = (T)dataContext;

                InitKey(context);

                ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
                var           entity        = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);
                EntityKey     entityKey     = objectContext.CreateEntityKey(EntitySetName, entity);

                if (objectContext.ObjectStateManager.TryGetObjectStateEntry(entityKey, out ObjectStateEntry objectStateEntry))
                {
                    objectStateEntry.ChangeState(EntityState.Deleted);
                }
                else
                {
                    if (_isCascade && !_isSelfReference)
                    {
                        context.Entry(entity).State = EntityState.Deleted;
                    }
                    else
                    {
                        var keyValues = new Object[entityKey.EntityKeyValues.Length];
                        for (int i = 0; i < keyValues.Length; i++)
                        {
                            keyValues[i] = entityKey.EntityKeyValues[i].Value;
                        }

                        IDbSet <TEntity> dbSet = _getEntitySet(context);
                        context.Entry(dbSet.Find(keyValues)).State = EntityState.Deleted;
                    }
                }
            }
            public override void AttachEntity(Object dataContext, ODataResourceBase entry)
            {
                var context = (T)dataContext;
                InternalEntityEntry internalEntry = GetEntityEntry(context, entry);

                if (internalEntry == null)
                {
                    TEntity entity = CreateEntity(context, entry);
                    _getEntitySet(context).Attach(entity);
                    internalEntry = _getEntitySet(context).Attach(entity).GetInfrastructure();

                    IKey key = _entityType.FindPrimaryKey();
                    foreach (ODataProperty odataProperty in entry.Properties)
                    {
                        IProperty property = _entityType.FindProperty(odataProperty.Name);
                        if (!key.Properties.Contains(property))
                        {
                            internalEntry.SetPropertyModified(property);
                        }
                    }
                }
                else
                {
                    foreach (ODataProperty odataProperty in entry.Properties)
                    {
                        IProperty property = _entityType.FindProperty(odataProperty.Name);
                        Object    value    = OeEdmClrHelper.GetClrValue(property.ClrType, odataProperty.Value);
                        internalEntry.SetProperty(property, value);
                    }
                }
            }
 /// <summary>
 /// Constructs an instance of <see cref="ODataResourceMetadataContextWithoutModel"/>.
 /// </summary>
 /// <param name="resource">The resource instance.</param>
 /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param>
 /// <param name="serializationInfo">The serialization info of the resource for writing without model.</param>
 /// <param name="requiresId">True if the resource requires an id, false if it is a non-deleted resource in a delta request payload.</param>
 internal ODataResourceMetadataContextWithoutModel(ODataResourceBase resource, IODataResourceTypeContext typeContext, ODataResourceSerializationInfo serializationInfo, bool requiresId)
     : base(resource, typeContext)
 {
     Debug.Assert(serializationInfo != null, "serializationInfo != null");
     this.serializationInfo = serializationInfo;
     this.requiresId        = requiresId;
 }
Esempio n. 4
0
        /// <summary>
        /// Writes the metadata properties for a resource which can only occur at the end.
        /// </summary>
        /// <param name="resourceState">The resource state for which to write the metadata properties.</param>
        /// <param name="duplicatePropertyNameChecker">The DuplicatePropertyNameChecker to use.</param>
        internal void WriteResourceEndMetadataProperties(IODataJsonLightWriterResourceState resourceState, IDuplicatePropertyNameChecker duplicatePropertyNameChecker)
        {
            Debug.Assert(resourceState != null, "resourceState != null");

            ODataResourceBase resource = resourceState.Resource;

            var navigationLinkInfo = resource.MetadataBuilder.GetNextUnprocessedNavigationLink();

            while (navigationLinkInfo != null)
            {
                Debug.Assert(resource.MetadataBuilder != null, "resource.MetadataBuilder != null");
                navigationLinkInfo.NestedResourceInfo.MetadataBuilder = resource.MetadataBuilder;

                this.WriteNavigationLinkMetadata(navigationLinkInfo.NestedResourceInfo, duplicatePropertyNameChecker);
                navigationLinkInfo = resource.MetadataBuilder.GetNextUnprocessedNavigationLink();
            }

            // write "odata.actions" metadata
            IEnumerable <ODataAction> actions = resource.Actions;

            if (actions != null && actions.Any())
            {
                this.WriteOperations(actions.Cast <ODataOperation>(), /*isAction*/ true);
            }

            // write "odata.functions" metadata
            IEnumerable <ODataFunction> functions = resource.Functions;

            if (functions != null && functions.Any())
            {
                this.WriteOperations(functions.Cast <ODataOperation>(), /*isAction*/ false);
            }
        }
        /// <summary>
        /// Get key value pair array for specific odata resource using specific entity type
        /// </summary>
        /// <param name="resource">The resource instance.</param>
        /// <param name="serializationInfo">The serialization info of the resource for writing without model.</param>
        /// <param name="actualEntityType">The edm entity type of the resource</param>
        /// <returns>Key value pair array</returns>
        internal static KeyValuePair <string, object>[] GetKeyProperties(
            ODataResourceBase resource,
            ODataResourceSerializationInfo serializationInfo,
            IEdmEntityType actualEntityType)
        {
            KeyValuePair <string, object>[] keyProperties = null;
            string actualEntityTypeName = null;

            if (serializationInfo != null)
            {
                if (String.IsNullOrEmpty(resource.TypeName))
                {
                    throw new ODataException(Strings.ODataResourceTypeContext_ODataResourceTypeNameMissing);
                }

                actualEntityTypeName = resource.TypeName;
                keyProperties        = ODataResourceMetadataContextWithoutModel.GetPropertiesBySerializationInfoPropertyKind(resource, ODataPropertyKind.Key, actualEntityTypeName);
            }
            else
            {
                keyProperties = GetPropertyValues(actualEntityType.Key(), resource, actualEntityType, /*isKeyProperty*/ true, /*isRequired*/ true).ToArray();
            }

            ValidateEntityTypeHasKeyProperties(keyProperties, actualEntityTypeName);
            return(keyProperties);
        }
        /// <summary>
        /// Get key value pair array for specific odata resource using specific entity type
        /// </summary>
        /// <param name="resource">The resource instance.</param>
        /// <param name="serializationInfo">The serialization info of the resource for writing without model.</param>
        /// <param name="actualEntityType">The edm entity type of the resource</param>
        /// <param name="requiresId">Whether key properties are required to be returned</param>
        /// <returns>Key value pair array</returns>
        internal static KeyValuePair <string, object>[] GetKeyProperties(
            ODataResourceBase resource,
            ODataResourceSerializationInfo serializationInfo,
            IEdmEntityType actualEntityType,
            bool requiresId)
        {
            Debug.Assert(resource != null, "GetKeyProperties called for a null resource.");

            KeyValuePair <string, object>[] keyProperties = null;
            string actualEntityTypeName = string.IsNullOrEmpty(resource.TypeName) ? actualEntityType?.FullName() : resource.TypeName;

            // if we have serializationInfo, try that first
            if (serializationInfo != null)
            {
                keyProperties = ODataResourceMetadataContextWithoutModel.GetPropertiesBySerializationInfoPropertyKind(resource, ODataPropertyKind.Key, actualEntityTypeName);
            }

            // if we didn't get any keys from serializationInfo, try using entity type
            if ((keyProperties == null || keyProperties.Length == 0) && actualEntityType != null)
            {
                keyProperties = GetPropertyValues(actualEntityType.Key(), resource, actualEntityType, requiresId).ToArray();
            }

            if (!ValidateEntityTypeHasKeyProperties(keyProperties, actualEntityTypeName, requiresId))
            {
                return(Enumerable.Empty <KeyValuePair <string, object> >().ToArray());
            }

            return(keyProperties);
        }
Esempio n. 7
0
        public override void AddEntity(Object dataContext, ODataResourceBase entry)
        {
            var entity = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);

            AddInstanceAnnotation(entry, entity);
            GetTable(dataContext).Insert(entity);
        }
Esempio n. 8
0
            public override void AttachEntity(Object dataContext, ODataResourceBase entry)
            {
                var           entity        = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);
                ObjectContext objectContext = ((IObjectContextAdapter)dataContext).ObjectContext;
                EntityKey     entityKey     = objectContext.CreateEntityKey(EntitySetName, entity);

                if (objectContext.ObjectStateManager.TryGetObjectStateEntry(entityKey, out ObjectStateEntry objectStateEntry))
                {
                    foreach (ODataProperty odataProperty in entry.Properties)
                    {
                        if (Array.Find(objectStateEntry.EntityKey.EntityKeyValues, k => k.Key == odataProperty.Name) == null)
                        {
                            int i = objectStateEntry.CurrentValues.GetOrdinal(odataProperty.Name);
                            objectStateEntry.CurrentValues.SetValue(i, odataProperty.Value);
                        }
                    }
                }
                else
                {
                    var context = (T)dataContext;
                    _getEntitySet(context).Attach(entity);
                    objectContext.ObjectStateManager.TryGetObjectStateEntry(entityKey, out objectStateEntry);

                    foreach (ODataProperty odataProperty in entry.Properties)
                    {
                        if (Array.Find(objectStateEntry.EntityKey.EntityKeyValues, k => k.Key == odataProperty.Name) == null)
                        {
                            objectStateEntry.SetModifiedProperty(odataProperty.Name);
                        }
                    }
                }
            }
        /// <summary>
        /// Get key value pair array for specific odata resource using specific entity type
        /// </summary>
        /// <param name="resource">The resource instance.</param>
        /// <param name="serializationInfo">The serialization info of the resource for writing without model.</param>
        /// <param name="actualEntityType">The edm entity type of the resource</param>
        /// <returns>Key value pair array</returns>
        internal static KeyValuePair <string, object>[] GetKeyProperties(
            ODataResourceBase resource,
            ODataResourceSerializationInfo serializationInfo,
            IEdmEntityType actualEntityType)
        {
            KeyValuePair <string, object>[] keyProperties = null;
            string actualEntityTypeName = null;

            if (serializationInfo != null)
            {
                if (String.IsNullOrEmpty(resource.TypeName))
                {
                    throw new ODataException(Strings.ODataResourceTypeContext_ODataResourceTypeNameMissing);
                }

                actualEntityTypeName = resource.TypeName;
                keyProperties        = ODataResourceMetadataContextWithoutModel.GetPropertiesBySerializationInfoPropertyKind(resource, ODataPropertyKind.Key, actualEntityTypeName);
            }
            else
            {
                actualEntityTypeName = actualEntityType.FullName();

                IEnumerable <IEdmStructuralProperty> edmKeyProperties = actualEntityType.Key();
                if (edmKeyProperties != null)
                {
                    keyProperties = edmKeyProperties.Select(p => new KeyValuePair <string, object>(p.Name, GetPrimitiveOrEnumPropertyValue(resource, p.Name, actualEntityTypeName, /*isKeyProperty*/ false))).ToArray();
                }
            }

            ValidateEntityTypeHasKeyProperties(keyProperties, actualEntityTypeName);
            return(keyProperties);
        }
Esempio n. 10
0
            public override void AddEntity(Object dataContext, ODataResourceBase entry)
            {
                IDbSet <TEntity> dbSet = _getEntitySet((T)dataContext);
                var entity             = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);

                dbSet.Add(entity);
            }
Esempio n. 11
0
        private InternalEntityEntry GetEntityEntry(DbContext context, ODataResourceBase entity)
        {
            Initialize(context);
            IStateManager stateManager = ((IDbContextDependencies)context).StateManager;

            return(stateManager.TryGetEntry(_entityType.FindPrimaryKey(), GetKeyValues(entity)));
        }
Esempio n. 12
0
        public override void AttachEntity(Object dataContext, ODataResourceBase entry)
        {
            var dbContext = (DbContext)dataContext;
            InternalEntityEntry internalEntry = GetEntityEntry(dbContext, entry);

            if (internalEntry == null)
            {
                Object      entity      = CreateEntity(dbContext, entry);
                EntityEntry entityEntry = dbContext.Attach(entity);
                AddInstanceAnnotation(entry, entityEntry);

                IReadOnlyList <IProperty> keyProperties = _entityType.FindPrimaryKey().Properties;
                foreach (ODataProperty odataProperty in entry.Properties)
                {
                    IProperty property = _entityType.FindProperty(odataProperty.Name);
                    if (!keyProperties.Contains(property))
                    {
                        entityEntry.Property(property.Name).IsModified = true;
                    }
                }
            }
            else
            {
                foreach (ODataProperty odataProperty in entry.Properties)
                {
                    IProperty property = _entityType.FindProperty(odataProperty.Name);
                    Object    value    = OeEdmClrHelper.GetClrValue(property.ClrType, odataProperty.Value);
                    internalEntry.SetProperty(property, value, false);
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of <see cref="ODataResourceWrapper"/>.
        /// </summary>
        /// <param name="resource">The wrapped resource item, it could be null.</param>
        public ODataResourceWrapper(ODataResourceBase resource)
        {
            Resource = resource;

            IsDeletedResource = resource != null && resource is ODataDeletedResource;

            NestedResourceInfos = new List <ODataNestedResourceInfoWrapper>();
        }
Esempio n. 14
0
            private InternalEntityEntry GetEntityEntry(T context, ODataResourceBase entity)
            {
                Initialize(context);
                var buffer       = new ValueBuffer(GetKeyValues(entity));
                var stateManager = (IInfrastructure <IStateManager>)context.ChangeTracker;

                return(stateManager.Instance.TryGetEntry(_entityType.FindPrimaryKey(), buffer, false));
            }
        /// <summary>
        /// Constructs an instance of <see cref="ODataResourceMetadataContext"/>.
        /// </summary>
        /// <param name="resource">The resource instance.</param>
        /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param>
        protected ODataResourceMetadataContext(ODataResourceBase resource, IODataResourceTypeContext typeContext)
        {
            Debug.Assert(resource != null, "resource != null");
            Debug.Assert(typeContext != null, "typeContext != null");

            this.resource    = resource;
            this.typeContext = typeContext;
        }
 /// <summary>
 /// Creates the metadata builder for the given resource. If such a builder is set, asking for payload
 /// metadata properties (like EditLink) of the resource may return a value computed by convention,
 /// depending on the metadata level and whether the user manually set an edit link or not.
 /// </summary>
 /// <param name="resource">The resource to create the metadata builder for.</param>
 /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource or resource set.</param>
 /// <param name="serializationInfo">The serialization info for the resource.</param>
 /// <param name="actualResourceType">The structured type of the resource.</param>
 /// <param name="selectedProperties">The selected properties of this scope.</param>
 /// <param name="isResponse">true if the resource metadata builder to create should be for a response payload; false for a request.</param>
 /// <param name="keyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.
 /// A null value means the user hasn't specified a preference and we should look for an annotation in the entity container, if available.</param>
 /// <param name="odataUri">The OData Uri.</param>
 /// <returns>The created metadata builder.</returns>
 internal abstract ODataResourceMetadataBuilder CreateResourceMetadataBuilder(
     ODataResourceBase resource,
     IODataResourceTypeContext typeContext,
     ODataResourceSerializationInfo serializationInfo,
     IEdmStructuredType actualResourceType,
     SelectedPropertiesNode selectedProperties,
     bool isResponse,
     bool keyAsSegment,
     ODataUri odataUri);
Esempio n. 17
0
        protected override Object CreateEntity(ODataResourceBase resource, IReadOnlyList <NavigationInfo> navigationProperties)
        {
            Db.OeEntitySetAdapter entitySetAdapter = TestHelper.FindEntitySetAdapterByTypeName(EntitySetAdapters, resource.TypeName);
            Type entityType = entitySetAdapter.EntityType;
            var  openType   = (SortedDictionary <String, Object>)CreateEntity(entityType, resource);

            Dictionary <PropertyInfo, NavigationInfo> propertyInfos = null;

            foreach (NavigationInfo navigationInfo in navigationProperties)
            {
                Object value = navigationInfo.Value;

                if (navigationInfo.Count != null || navigationInfo.NextPageLink != null)
                {
                    PropertyInfo clrProperty = entityType.GetProperty(navigationInfo.Name);
                    if (value == null && navigationInfo.NextPageLink != null)
                    {
                        if (navigationInfo.IsCollection)
                        {
                            value = new List <SortedDictionary <String, Object> >();
                        }
                        else
                        {
                            value = new SortedDictionary <String, Object>();
                        }
                    }
                    base.NavigationProperties.Add(value, navigationInfo);

                    if (propertyInfos == null)
                    {
                        propertyInfos = new Dictionary <PropertyInfo, NavigationInfo>(navigationProperties.Count);
                        base.NavigationInfoEntities.Add(openType, propertyInfos);
                    }
                    propertyInfos.Add(clrProperty, navigationInfo);
                }

                if (value == null)
                {
                    PropertyInfo clrProprety = entityType.GetProperty(navigationInfo.Name);
                    Type         type        = OeExpressionHelper.GetCollectionItemTypeOrNull(clrProprety.PropertyType);
                    if (type == null)
                    {
                        type = clrProprety.PropertyType;
                    }

                    if (OeExpressionHelper.IsEntityType(type))
                    {
                        value = DBNull.Value;
                    }
                }

                openType.Add(navigationInfo.Name, value);
            }

            return(openType);
        }
Esempio n. 18
0
            /// <summary>
            /// Constructs an instance of <see cref="ODataResourceMetadataContextWithModel"/>.
            /// </summary>
            /// <param name="resource">The resource instance.</param>
            /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param>
            /// <param name="actualResourceType">The structured type of the resource.</param>
            /// <param name="metadataContext">The metadata context to use.</param>
            /// <param name="selectedProperties">The selected properties.</param>
            internal ODataResourceMetadataContextWithModel(ODataResourceBase resource, IODataResourceTypeContext typeContext, IEdmStructuredType actualResourceType, IODataMetadataContext metadataContext, SelectedPropertiesNode selectedProperties)
                : base(resource, typeContext)
            {
                Debug.Assert(actualResourceType != null, "actualResourceType != null");
                Debug.Assert(metadataContext != null, "metadataContext != null");
                Debug.Assert(selectedProperties != null, "selectedProperties != null");

                this.actualResourceType = actualResourceType;
                this.metadataContext    = metadataContext;
                this.selectedProperties = selectedProperties;
            }
Esempio n. 19
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="resourceMetadataContext">The context to answer basic metadata questions about the resource.</param>
        /// <param name="metadataContext">The metadata context.</param>
        /// <param name="uriBuilder">The uri builder to use.</param>
        internal ODataConventionalResourceMetadataBuilder(IODataResourceMetadataContext resourceMetadataContext, IODataMetadataContext metadataContext, ODataUriBuilder uriBuilder)
        {
            Debug.Assert(resourceMetadataContext != null, "resourceMetadataContext != null");
            Debug.Assert(metadataContext != null, "metadataContext != null");
            Debug.Assert(uriBuilder != null, "uriBuilder != null");

            this.ResourceMetadataContext = resourceMetadataContext;
            this.UriBuilder      = uriBuilder;
            this.MetadataContext = metadataContext;
            this.ProcessedNestedResourceInfos = new HashSet <string>(StringComparer.Ordinal);
            this.resource = resourceMetadataContext.Resource;
        }
        /// <summary>
        /// Gets the value for a primitive or enum property.
        /// </summary>
        /// <param name="resource">The resource to get the property value.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="entityTypeName">The name of the entity type to get the property value.</param>
        /// <param name="isKeyProperty">true if the property is a key property, false otherwise.</param>
        /// <returns>The value of the property.</returns>
        private static object GetPrimitiveOrEnumPropertyValue(ODataResourceBase resource, string propertyName, string entityTypeName, bool isKeyProperty)
        {
            Debug.Assert(resource != null, "resource != null");

            ODataProperty property = resource.NonComputedProperties == null ? null : resource.NonComputedProperties.SingleOrDefault(p => p.Name == propertyName);

            if (property == null)
            {
                throw new ODataException(Strings.EdmValueUtils_PropertyDoesntExist(entityTypeName, propertyName));
            }

            return(GetPrimitiveOrEnumPropertyValue(entityTypeName, property, isKeyProperty));
        }
Esempio n. 21
0
 /// <summary>
 /// Creates the metadata builder for the given resource. If such a builder is set, asking for payload
 /// metadata properties (like EditLink) of the resource may return a value computed by convention,
 /// depending on the metadata level and whether the user manually set an edit link or not.
 /// </summary>
 /// <param name="resource">The resource to create the metadata builder for.</param>
 /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource or resource set.</param>
 /// <param name="serializationInfo">The serialization info for the resource.</param>
 /// <param name="actualResourceType">The structured type of the resource.</param>
 /// <param name="selectedProperties">The selected properties of this scope.</param>
 /// <param name="isResponse">true if the resource metadata builder to create should be for a response payload; false for a request.</param>
 /// <param name="keyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.</param>
 /// <param name="odataUri">The OData Uri.</param>
 /// <param name="settings">The settings to use when creating the resource builder.</param>
 /// <returns>The created metadata builder.</returns>
 internal override ODataResourceMetadataBuilder CreateResourceMetadataBuilder(
     ODataResourceBase resource,
     IODataResourceTypeContext typeContext,
     ODataResourceSerializationInfo serializationInfo,
     IEdmStructuredType actualResourceType,
     SelectedPropertiesNode selectedProperties,
     bool isResponse,
     bool keyAsSegment,
     ODataUri odataUri,
     ODataMessageWriterSettings settings)
 {
     return(ODataResourceMetadataBuilder.Null);
 }
Esempio n. 22
0
 public TestODataJsonLightWriterResourceState(
     ODataResourceBase resource,
     IEdmStructuredType structuredType,
     ODataResourceSerializationInfo serializationInfo,
     IEdmNavigationSource navigationSource,
     bool isUndeclared)
 {
     Resource                 = resource;
     ResourceType             = structuredType;
     ResourceTypeFromMetadata = structuredType;
     SerializationInfo        = serializationInfo;
     NavigationSource         = navigationSource;
     IsUndeclared             = isUndeclared;
 }
Esempio n. 23
0
        private Object CreateEntity(DbContext context, ODataResourceBase entry)
        {
            Initialize(context);

            var values = _valueBufferArrayInit();

            foreach (ODataProperty odataProperty in entry.Properties)
            {
                IProperty property = _entityType.FindProperty(odataProperty.Name);
                Object    value    = OeEdmClrHelper.GetClrValue(property.ClrType, odataProperty.Value);
                values[property.GetIndex()] = value;
            }
            return(_materializer(new MaterializationContext(new ValueBuffer(values), context)));
        }
        protected virtual Object CreateEntity(ODataResourceBase resource, IReadOnlyList <NavigationInfo> navigationProperties)
        {
            Db.OeEntitySetAdapter entitySetAdapter = TestHelper.FindEntitySetAdapterByTypeName(EntitySetAdapters, resource.TypeName);
            Object entity = CreateEntity(entitySetAdapter.EntityType, resource);
            Dictionary <PropertyInfo, NavigationInfo> propertyInfos = null;

            foreach (NavigationInfo navigationInfo in navigationProperties)
            {
                PropertyInfo clrProperty = entitySetAdapter.EntityType.GetProperty(navigationInfo.Name);
                Object       value       = navigationInfo.Value;

                if ((navigationInfo.Count == null && navigationInfo.NextPageLink == null))
                {
                    if (clrProperty.GetSetMethod() != null)
                    {
                        clrProperty.SetValue(entity, value);
                    }
                }
                else
                {
                    if (value == null && navigationInfo.NextPageLink != null)
                    {
                        if (navigationInfo.IsCollection)
                        {
                            value = CreateCollection(clrProperty.PropertyType);
                        }
                        else
                        {
                            value = Activator.CreateInstance(clrProperty.PropertyType);
                        }
                    }

                    clrProperty.SetValue(entity, value);
                    if (value != null)
                    {
                        NavigationProperties.Add(value, navigationInfo);
                    }

                    if (propertyInfos == null)
                    {
                        propertyInfos = new Dictionary <PropertyInfo, NavigationInfo>(navigationProperties.Count);
                        NavigationInfoEntities.Add(entity, propertyInfos);
                    }
                    propertyInfos.Add(clrProperty, navigationInfo);
                }
            }

            return(entity);
        }
Esempio n. 25
0
 /// <summary>
 /// Creates the metadata builder for the given resource. If such a builder is set, asking for payload
 /// metadata properties (like EditLink) of the resource may return a value computed by convention,
 /// depending on the metadata level and whether the user manually set an edit link or not.
 /// </summary>
 /// <param name="resource">The resource to create the metadata builder for.</param>
 /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource or resource set.</param>
 /// <param name="serializationInfo">The serialization info for the resource.</param>
 /// <param name="actualResourceType">The structured type of the resource.</param>
 /// <param name="selectedProperties">The selected properties of this scope.</param>
 /// <param name="isResponse">true if the resource metadata builder to create should be for a response payload; false for a request.</param>
 /// <param name="keyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.</param>
 /// <param name="odataUri">The OData Uri.</param>
 /// <returns>The created metadata builder.</returns>
 internal override ODataResourceMetadataBuilder CreateResourceMetadataBuilder(
     ODataResourceBase resource,
     IODataResourceTypeContext typeContext,
     ODataResourceSerializationInfo serializationInfo,
     IEdmStructuredType actualResourceType,
     SelectedPropertiesNode selectedProperties,
     bool isResponse,
     bool keyAsSegment,
     ODataUri odataUri)
 {
     // For minimal metadata we don't want to change the metadata builder that's currently on the resource because the resource might come from a JSON light
     // reader and it would contain the metadata builder from the reader.  Until we give the user the ability to choose whether to write what was reported
     // by the reader versus what was on the wire, we no-op here so the writer will just write what's on the OM for now.
     return(null);
 }
Esempio n. 26
0
        /// <summary>
        /// Creates an instance of <see cref="ODataResourceMetadataContext"/>.
        /// </summary>
        /// <param name="resource">The resource instance.</param>
        /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param>
        /// <param name="serializationInfo">The serialization info of the resource for writing without model.</param>
        /// <param name="actualResourceType">The structured type of the resource.</param>
        /// <param name="metadataContext">The metadata context to use.</param>
        /// <param name="selectedProperties">The selected properties.</param>
        /// <returns>A new instance of <see cref="ODataResourceMetadataContext"/>.</returns>
        internal static ODataResourceMetadataContext Create(
            ODataResourceBase resource,
            IODataResourceTypeContext typeContext,
            ODataResourceSerializationInfo serializationInfo,
            IEdmStructuredType actualResourceType,
            IODataMetadataContext metadataContext,
            SelectedPropertiesNode selectedProperties)
        {
            if (serializationInfo != null)
            {
                return(new ODataResourceMetadataContextWithoutModel(resource, typeContext, serializationInfo));
            }

            return(new ODataResourceMetadataContextWithModel(resource, typeContext, actualResourceType, metadataContext, selectedProperties));
        }
        public override void AttachEntity(Object dataContext, ODataResourceBase entry)
        {
            Object entity = FindEntity(GetSource(dataContext), entry);

            lock (entity)
                foreach (ODataProperty property in entry.Properties)
                {
                    if (Array.IndexOf(_keyNames, property.Name) == -1)
                    {
                        PropertyInfo propertyInfo = EntityType.GetProperty(property.Name) !;
                        Object       clrValue     = OeEdmClrHelper.GetClrValue(propertyInfo.PropertyType, property.Value);
                        propertyInfo.SetValue(entity, clrValue);
                    }
                }
        }
Esempio n. 28
0
        public static Object CreateEntity(Type clrType, ODataResourceBase entry)
        {
            Object entity = Activator.CreateInstance(clrType);

            foreach (ODataProperty property in entry.Properties)
            {
                PropertyInfo clrProperty = clrType.GetProperty(property.Name);
                if (clrProperty != null)
                {
                    Object value = GetClrValue(clrProperty.PropertyType, property.Value);
                    clrProperty.SetValue(entity, value);
                }
            }
            return(entity);
        }
Esempio n. 29
0
            public override void AddEntity(Object dataContext, ODataResourceBase entry)
            {
                var context = (DbContext)dataContext;
                EntityEntry <TEntity> entityEntry = context.Add(CreateEntity(context, entry));

                IReadOnlyList <IProperty> keyProperties = _entityType.FindPrimaryKey().Properties;

                for (int i = 0; i < keyProperties.Count; i++)
                {
                    if (keyProperties[i].ValueGenerated == ValueGenerated.OnAdd)
                    {
                        entityEntry.GetInfrastructure().MarkAsTemporary(keyProperties[i]);
                    }
                }
            }
Esempio n. 30
0
        public override void AddEntity(Object dataContext, ODataResourceBase entry)
        {
            var         context     = (DbContext)dataContext;
            EntityEntry entityEntry = context.Add(CreateEntity(context, entry));

            AddInstanceAnnotation(entry, entityEntry);

            IReadOnlyList <IProperty> keyProperties = _entityType.FindPrimaryKey().Properties;

            for (int i = 0; i < keyProperties.Count; i++)
            {
                if (keyProperties[i].ValueGenerated == ValueGenerated.OnAdd)
                {
                    entityEntry.Property(keyProperties[i].Name).IsTemporary = true;
                }
            }
        }