示例#1
0
        public ActionResult <ApiResult <bool> > CreateTables([FromForm] string model, [FromForm] int dbid)
        {
            var tableDb = base.GetTryDb(dbid);
            var result  = new ApiResult <bool>();

            if (!string.IsNullOrEmpty(model))
            {
                var list    = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTableViewModel> >(model);
                var oldList = CodeTableDb.AsQueryable().In(list.Select(it => it.Id).ToList()).ToList();
                base.Check(oldList.Any(it => it.IsLock), string.Join(",", oldList.Where(it => it.IsLock).Select(it => it.ClassName)) + "是锁表状态禁止建表");
                List <EntitiesGen> genList = GetGenList(oldList, CodeTypeDb.GetList(), tableDb.CurrentConnectionConfig.DbType);
                foreach (var item in genList)
                {
                    item.PropertyGens = item.PropertyGens.Where(it => it.IsIgnore == false).ToList();
                    foreach (var property in item.PropertyGens)
                    {
                        if (property.IsSpecialType)
                        {
                            property.Type = "string";
                        }
                    }
                }
                string key = TemplateHelper.EntityKey + SyntaxTreeHelper.TemplateString.GetHashCode();
                foreach (var item in genList)
                {
                    var classString = TemplateHelper.GetTemplateValue(key, SyntaxTreeHelper.TemplateString, item);
                    var type        = SyntaxTreeHelper.GetModelTypeByClass(classString, item.ClassName);
                    tableDb.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
                    {
                        EntityNameService = (type, info) =>
                        {
                            if (info.EntityName == item.ClassName || (info.EntityName == null && info.DbTableName == item.ClassName))
                            {
                                info.EntityName       = item.ClassName;
                                info.DbTableName      = item.TableName;
                                info.TableDescription = item.Description;
                            }
                        },
                        EntityService = (type, info) =>
                        {
                            if (info.EntityName == item.ClassName)
                            {
                                var column = item.PropertyGens.FirstOrDefault(it => it.PropertyName == info.PropertyName);
                                info.DbColumnName      = column.DbColumnName;
                                info.ColumnDescription = column.Description;
                                info.IsNullable        = column.IsNullable;
                                info.Length            = Convert.ToInt32(column.Length);
                                info.DecimalDigits     = Convert.ToInt32(column.DecimalDigits);
                                info.IsPrimarykey      = column.IsPrimaryKey;
                                info.IsIdentity        = column.IsIdentity;
                                info.IsIgnore          = column.IsIgnore;
                                info.DataType          = column.DbType;
                                if (tableDb.CurrentConnectionConfig.DbType == DbType.Sqlite && info.IsIdentity)
                                {
                                    info.DataType = "integer";
                                }
                            }
                        }
                    };
                    tableDb.CodeFirst.InitTables(type);
                }
            }
            result.IsSuccess = true;
            return(result);
        }