示例#1
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);
            }
示例#2
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);
                        }
                    }
                }
            }
示例#3
0
        public override void AddEntity(Object dataContext, ODataResourceBase entry)
        {
            var entity = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);

            AddInstanceAnnotation(entry, entity);
            GetTable(dataContext).Insert(entity);
        }
示例#4
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;
                    }
                }
            }
        protected Object CreateEntity(ODataResource resource, IReadOnlyList <NavigationInfo> navigationProperties)
        {
            Db.OeEntitySetAdapter entitySetAdapter = TestHelper.FindEntitySetAdapterByTypeName(EntitySetAdapters, resource.TypeName);
            Object entity = OeEdmClrHelper.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);
        }
        internal static Object CreateEntity(OeOperationMessage operation, Type clrEntityType)
        {
            if (operation.Method == ODataConstants.MethodPatch)
            {
                var properties = new Dictionary <String, Object>();
                foreach (ODataProperty odataProperty in operation.Entry.Properties)
                {
                    PropertyInfo?propertyInfo = clrEntityType.GetProperty(odataProperty.Name);
                    if (propertyInfo == null)
                    {
                        throw new InvalidOperationException("Not found property " + odataProperty.Name + " in type " + clrEntityType.FullName);
                    }

                    properties[odataProperty.Name] = OeEdmClrHelper.GetClrValue(propertyInfo.PropertyType, odataProperty.Value);
                }
                return(properties);
            }

            return(OeEdmClrHelper.CreateEntity(clrEntityType, operation.Entry));
        }
示例#7
0
        protected Object CreateEntity(ODataResource resource, IReadOnlyList <NavigationProperty> navigationProperties)
        {
            Db.OeEntitySetAdapter entitySetAdapter = EntitySetAdapters.FindByTypeName(resource.TypeName);
            Object entity = OeEdmClrHelper.CreateEntity(entitySetAdapter.EntityType, resource);
            Dictionary <PropertyInfo, ODataResourceSetBase> propertyInfos = null;

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

                if (navigationProperty.ResourceSet == null || (navigationProperty.ResourceSet.Count == null && navigationProperty.ResourceSet.NextPageLink == null))
                {
                    if (clrProperty.GetSetMethod() != null)
                    {
                        clrProperty.SetValue(entity, value);
                    }
                    continue;
                }

                if (value == null && navigationProperty.ResourceSet.NextPageLink != null)
                {
                    value = CreateCollection(clrProperty.PropertyType);
                }

                clrProperty.SetValue(entity, value);
                if (value is IEnumerable collection)
                {
                    NavigationProperties.Add(collection, navigationProperty.ResourceSet);

                    if (propertyInfos == null)
                    {
                        propertyInfos = new Dictionary <PropertyInfo, ODataResourceSetBase>(navigationProperties.Count);
                        NavigationPropertyEntities.Add(entity, propertyInfos);
                    }
                    propertyInfos.Add(clrProperty, navigationProperty.ResourceSet);
                }
            }

            return(entity);
        }
 protected virtual Object CreateEntity(Type entityType, ODataResourceBase resource)
 {
     return(OeEdmClrHelper.CreateEntity(entityType, resource));
 }
示例#9
0
        protected virtual async Task BatchCore()
        {
            var actionDescriptors    = GetService <IActionDescriptorCollectionProvider>().ActionDescriptors;
            var actionInvokerFactory = GetService <IActionInvokerFactory>();

            String basePath = "";

            if (base.HttpContext.Request.PathBase.HasValue)
            {
                basePath = base.HttpContext.Request.PathBase;
            }
            else
            {
                int i = base.HttpContext.Request.Path.Value.IndexOf('/', 1);
                if (i > 0)
                {
                    basePath = base.HttpContext.Request.Path.Value.Substring(0, i);
                }
            }
            Uri baseUri = UriHelper.GetBaseUri(base.Request);

            OeBatchMessage batchMessage = OeBatchMessage.CreateBatchMessage(EdmModel, baseUri, base.HttpContext.Request.Body, base.HttpContext.Request.ContentType);
            Object         dataContext  = null;

            try
            {
                dataContext = DataAdapter.CreateDataContext();

                foreach (OeOperationMessage operation in batchMessage.Changeset)
                {
                    OeEntitySetAdapter entitySetAdapter = DataAdapter.EntitySetAdapters.FindByEntitySetName(operation.EntitySet.Name);
                    String             path             = basePath + "/" + entitySetAdapter.EntitySetName;

                    List <ActionDescriptor> candidates = OeRouter.SelectCandidates(actionDescriptors.Items, base.RouteData.Values, path, operation.Method);
                    if (candidates.Count > 1)
                    {
                        throw new AmbiguousActionException(String.Join(Environment.NewLine, candidates.Select(c => c.DisplayName)));
                    }
                    if (candidates.Count == 0)
                    {
                        throw new InvalidOperationException("Action " + operation.Method + " for controller " + basePath + " not found");
                    }

                    Object entity;
                    if (operation.Method == ODataConstants.MethodPatch)
                    {
                        var properties = new Dictionary <String, Object>();
                        foreach (ODataProperty odataProperty in operation.Entry.Properties)
                        {
                            PropertyInfo propertyInfo = entitySetAdapter.EntityType.GetProperty(odataProperty.Name);
                            properties[odataProperty.Name] = OeEdmClrHelper.GetClrValue(propertyInfo.PropertyType, odataProperty.Value);
                        }
                        entity = properties;
                    }
                    else
                    {
                        entity = OeEdmClrHelper.CreateEntity(entitySetAdapter.EntityType, operation.Entry);
                    }

                    var modelState = new OeFilterAttribute.BatchModelStateDictionary()
                    {
                        Entity      = entity,
                        DataContext = new OeDataContext(entitySetAdapter, EdmModel, dataContext, operation)
                    };
                    OnBeforeInvokeController(modelState.DataContext, operation.Entry);

                    var            actionContext = new ActionContext(base.HttpContext, base.HttpContext.GetRouteData(), candidates[0], modelState);
                    IActionInvoker actionInvoker = actionInvokerFactory.CreateInvoker(actionContext);
                    await actionInvoker.InvokeAsync();
                }

                await SaveChangesAsync(dataContext).ConfigureAwait(false);
            }
            finally
            {
                if (dataContext != null)
                {
                    DataAdapter.CloseDataContext(dataContext);
                }
            }

            base.HttpContext.Response.ContentType = base.HttpContext.Request.ContentType;
            var batchWriter = new OeBatchWriter(EdmModel, baseUri);

            batchWriter.Write(base.HttpContext.Response.Body, batchMessage);
        }
示例#10
0
            public override void RemoveEntity(Object dataContext, ODataResourceBase entry)
            {
                var entity = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);

                GetTable(dataContext).Delete(entity);
            }
示例#11
0
            public override void AttachEntity(Object dataContext, ODataResourceBase entry)
            {
                var entity = (TEntity)OeEdmClrHelper.CreateEntity(EntityType, entry);

                GetTable(dataContext).Update(entity, entry.Properties.Select(p => p.Name));
            }