Exemple #1
0
        private void GetDBModelObject()
        {
            using (WebDbPoliticsModel context = new WebDbPoliticsModel())
            {
                DbSet set = context.Set(EntityType);

                //var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
                //return objectStateEntry.EntityKey.EntityKeyValues[0].Value;

                _dbModelObject = set.Find(_modelObject.GetId());
            }
        }
        private object WriteEntityToDb(AddEntityRequest message)
        {
            Newtonsoft.Json.Linq.JObject jsonObject = message.ModelObject as Newtonsoft.Json.Linq.JObject;

            var entityAssembly = Assembly.GetAssembly(typeof(EntityFramework.WebDbPoliticsModel));
            Type entityType = entityAssembly.GetTypes().FirstOrDefault(t => t.Name == message.EntityType);
            object modelObject = jsonObject.ToObject(entityType);

            var modelType = modelObject.GetType();

            using (WebDbPoliticsModel db = new WebDbPoliticsModel())
            {
                db.Configuration.ProxyCreationEnabled = false;
                DbSet set = db.Set(modelType);
                var resultObject = set.Add(modelObject);
                db.SaveChanges();
                return resultObject;
            }
        }
Exemple #3
0
 private object LoadEntityFromDb()
 {
     using (WebDbPoliticsModel context = new WebDbPoliticsModel())
     {
         context.Configuration.ProxyCreationEnabled = false;
         DbSet set = context.Set(EntityType);
         return set.Find(Id);
     }
 }
Exemple #4
0
        private void SaveModelObject()
        {
            if (_dbModelObject == null)
                GetDBModelObject();

            using (WebDbPoliticsModel context = new WebDbPoliticsModel())
            {
                DbSet set = context.Set(EntityType);
                //var existing = set.Find(Id);

                var entry = context.Entry(_dbModelObject);
                set.Attach(_dbModelObject);
                entry.CurrentValues.SetValues(_modelObject);
                //entry.State = EntityState.Modified;

                var navigationProperties = (_modelObject as IModelObject).GetNavigationProperties();
                foreach (var property in navigationProperties)
                {
                    Type navigationType = property.PropertyType;
                    DbSet navSet = context.Set(navigationType.GenericTypeArguments.First());
                    var navigationItemCollection = property.GetValue(_modelObject) as IEnumerable;
                    foreach (var navigationItem in navigationItemCollection)
                    {
                        long id = navigationItem.GetId();
                        if (id > 0)
                        {
                            var existingNavigationItem = navSet.Find(id);
                            context.Entry(existingNavigationItem).CurrentValues.SetValues(navigationItem);
                        }
                        else
                        {
                            navSet.Add(navigationItem);
                        }
                    }
                    //navSet.Attach(navigationItem);
                }
                //context.UpdateGraph(_modelObject);

                // Add extension method for getting navigation properties of a modelobject
                // Then iterate them here and save any that are modified.

                //set.Add(_modelObject);
                //context.Entry(_modelObject).State = EntityState.Modified;
                var validationErrors = context.GetValidationErrors();
                //existing = _modelObject;
                context.SaveChanges();

                _dbModelObject = _modelObject;
            }
        }
        public GenericDBQueryActor()
        {
            Receive<GetAllRequest>(message =>
            {
                using (WebDbPoliticsModel model = new WebDbPoliticsModel())
                {
                    model.Configuration.ProxyCreationEnabled = false;
                    var entityAssembly = Assembly.GetAssembly(typeof(IModelObject)); // was WebDbPoliticsModel before removing dtos
                    var allEntityTypes = entityAssembly.GetTypes().ToList();
                    Type type = allEntityTypes.FirstOrDefault(t => t.Name == message.EntityType);

                    if (type == null)
                    {
                        Sender.Tell("ERROR");
                        return;
                    }

                    var method = typeof(WebDbPoliticsModel).GetMethod("Set", new Type[0]).MakeGenericMethod(type);
                    var set = method.Invoke(model, new object[0]);
                    DbSet<Position> dbSet = set as DbSet<Position>;

                    //DbSet<IModelObject> set = model.Set(type).Cast<IModelObject>();//.Include("PollIssues");

                    var relationshipCollectionProperties = type.GetProperties()
                  .Where(t =>
                          t.Name != "Relationships" &&
                          t.GetGetMethod().IsVirtual &&
                          t.PropertyType.IsGenericType &&
                          t.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
                  .ToList();

                    var query = dbSet.AsQueryable();

                    foreach (var relatedProperty in relationshipCollectionProperties)
                    {
                        query = query.Include(relatedProperty.Name);
                    }
                    ///set.AsQueryable().

                    var relationshipSingleProperties = type.GetProperties()
                                            .Where(t => t.GetGetMethod().IsVirtual &&
                                            t.PropertyType.IsGenericType == false &&
                                            t.DeclaringType != typeof(ModelObjectBase))
                                            .ToList();

                    foreach(var relatedProperty in relationshipSingleProperties)
                    {
                        query = query.Include(relatedProperty.Name);
                    }

                    if (!String.IsNullOrWhiteSpace(message.Filter))
                    {
                        query = query.Where(t => t.DesignerId.Contains(message.Filter));
                    }

                    var result = query.AsNoTracking().ToListAsync().Result;//.ConvertAll<object>();
                    List<object> resultsAsObjectList = new List<object>();
                    foreach (var r in result)
                        result.Add(r);

                    Sender.Tell(new AkkaSearchResults(message.EntityType, resultsAsObjectList));

                }
            });
        }