Example #1
0
        public static DbContextEntityResult Error(List <string> errors)
        {
            DbContextEntityResult result = new DbContextEntityResult();

            result.Errors = errors;
            return(result);
        }
Example #2
0
        public static DbContextEntityResult Error(string error)
        {
            DbContextEntityResult result = new DbContextEntityResult();

            result.Errors.Add(error);
            return(result);
        }
Example #3
0
        public DbContextEntityResult GetEntityByPrimaryKey(HttpContext httpContext, string contextName, DbContextEntityInfo entityInfo, Dictionary <string, string> pkValues)
        {
            Type      dbContextType = GetDbContextTypes().FirstOrDefault(y => y.Name == contextName);
            DbContext dbContext     = httpContext?.RequestServices?.GetService(dbContextType) as DbContext;

            if (dbContext == null)
            {
                return(DbContextEntityResult.Error("Cannot find DbContext: " + contextName));
            }

            try
            {
                var primaryKeys = entityInfo.Fields
                                  .Where(x => x.IsPrimaryKey)
                                  .Select(x => Binder.ConvertToType(pkValues.FirstOrDefault(v => v.Key == x.Name).Value, x.FieldInfo.FieldType))
                                  .ToArray();

                var entity = dbContext.Find(entityInfo.ClrType, primaryKeys);
                if (entity == null)
                {
                    var pkString = string.Join(";", pkValues.Select(x => x.Key + "=" + x.Value).ToArray());
                    return(DbContextEntityResult.Error($"Cannot find entity {entityInfo.ClrTypeName} with primary key: {pkString}"));
                }
                var entityEntry  = dbContext.Entry(entity);
                var fieldsValues = entityInfo.Fields.ToDictionary(
                    x => x.Name,
                    x => entityEntry.Property(x.Name).CurrentValue);
                return(new DbContextEntityResult
                {
                    Entity = entity,
                    FieldsValues = fieldsValues
                });
            } catch (Exception ex)
            {
                var pkString = string.Join(";", pkValues.Select(x => x.Key + "=" + x.Value).ToArray());
                return(DbContextEntityResult.Error($"Cannot find entity {entityInfo.ClrTypeName} with primary key: {pkString}. ex.GetDetails()"));
            }
        }
Example #4
0
        public DbContextEntityResult Update(HttpContext httpContext, string contextName, DbContextEntityInfo entityInfo, Dictionary <string, string> pkValues, Dictionary <string, string> fieldsValues)
        {
            Type      dbContextType = GetDbContextTypes().FirstOrDefault(y => y.Name == contextName);
            DbContext dbContext     = httpContext?.RequestServices?.GetService(dbContextType) as DbContext;

            if (dbContext == null)
            {
                return(DbContextEntityResult.Error("Cannot find DbContext: " + contextName));
            }

            var entityResult = GetEntityByPrimaryKey(httpContext, contextName, entityInfo, pkValues);

            if (entityResult.IsSuccess == false)
            {
                return(DbContextEntityResult.Error(entityResult.Errors));
            }
            object entity = entityResult.Entity;

            DbContextEntityResult result = new DbContextEntityResult();

            result.Entity = entity;
            var entityEntry = dbContext.Entry(entity);

            foreach (var field in entityInfo.Fields)
            {
                object value = fieldsValues.FirstOrDefault(x => x.Key == field.Name).Value;
                try
                {
                    if (Binder.IsConvertionSupported(field.ClrType) == false)
                    {
                        result.Warnings.Add($"Type of property {field.Name} is not supported.");
                        continue;
                    }
                    value = Binder.ConvertToType(value, field.ClrType);
                    entityEntry.Property(field.Name).CurrentValue = value;
                }
                catch (Exception ex)
                {
                    result.Errors.Add($"Cannot set entity property {field.Name} to {value}. Error: {ex.GetAllMessages()}");
                }
            }

            if (result.Errors.Count > 0)
            {
                return(result);
            }

            try {
                dbContext.Update(entity);
            }
            catch (Exception ex)
            {
                result.Errors.Add($"Cannot update entity in the context. Error: {ex.GetAllMessages()}");
                return(result);
            }

            try
            {
                dbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                result.Errors.Add($"Cannot save entity. Error: {ex.GetAllMessages()}");
                return(result);
            }

            return(result);
        }
Example #5
0
        public DbContextEntityResult Add(HttpContext httpContext, string contextName, DbContextEntityInfo entityInfo, Dictionary <string, string> fieldsValues)
        {
            Type      dbContextType = GetDbContextTypes().FirstOrDefault(y => y.Name == contextName);
            DbContext dbContext     = httpContext?.RequestServices?.GetService(dbContextType) as DbContext;

            if (dbContext == null)
            {
                return(DbContextEntityResult.Error("Cannot find DbContext: " + contextName));
            }

            var entity = Activator.CreateInstance(entityInfo.ClrType);
            DbContextEntityResult result = new DbContextEntityResult();

            result.Entity = entity;
            var entityEntry = dbContext.Entry(entity);

            foreach (var field in fieldsValues)
            {
                try
                {
                    var entityField = entityInfo.Fields.FirstOrDefault(x => x.Name == field.Key);
                    if (entityField == null)
                    {
                        result.Errors.Add($"Property {field.Key} does not exist in the entity {entityInfo.ClrTypeName}");
                        continue;
                    }
                    var value = Binder.ConvertToType(field.Value, entityField.ClrType);
                    entityEntry.Property(field.Key).CurrentValue = value;
                }catch (Exception ex)
                {
                    result.Errors.Add($"Cannot set entity property {field.Key} to {field.Value}. Error: {ex.GetAllMessages()}");
                }
            }

            if (result.Errors.Count > 0)
            {
                return(result);
            }

            try
            {
                dbContext.Add(entity);
            }
            catch (Exception ex)
            {
                result.Errors.Add($"Cannot add entity to the context. Error: {ex.GetAllMessages()}");
                return(result);
            }

            try
            {
                dbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                result.Errors.Add($"Cannot save entity. Error: {ex.GetAllMessages()}");
                return(result);
            }

            return(result);
        }