Beispiel #1
0
        public List <TableInformationModel> GetTableDefinitions()
        {
            var consolidator = new DacpacConsolidate.DacpacConsolidator();

            var dacpacPath = consolidator.Consolidate(_dacpacPath);

            using (var model = new TSqlTypedModel(dacpacPath))
            {
                var result = model.GetObjects <TSqlTable>(DacQueryScopes.UserDefined)
                             .Select(m => new TableInformationModel($"[{m.Name.Parts[0]}].[{m.Name.Parts[1]}]", m.PrimaryKeyConstraints.Any(), false))
                             .ToList();

                return(result);
            }
        }
        public virtual DatabaseModel Create(string dacpacPath, IEnumerable <string> tables, IEnumerable <string> schemas)
        {
            if (string.IsNullOrEmpty(dacpacPath))
            {
                throw new ArgumentException(@"invalid path", nameof(dacpacPath));
            }
            if (!File.Exists(dacpacPath))
            {
                throw new ArgumentException("Dacpac file not found");
            }

            var dbModel = new DatabaseModel
            {
                DatabaseName  = Path.GetFileNameWithoutExtension(dacpacPath),
                DefaultSchema = schemas.Count() > 0 ? schemas.First() : "dbo"
            };
            //Sequences not created - not needed for scaffolding

            var consolidator = new DacpacConsolidate.DacpacConsolidator();

            dacpacPath = consolidator.Consolidate(dacpacPath);

            var model = new TSqlTypedModel(dacpacPath);

            var typeAliases = GetTypeAliases(model, dbModel);

            var items = model.GetObjects <TSqlTable>(DacQueryScopes.UserDefined)
                        .Where(t => t.PrimaryKeyConstraints.Any() &&
                               !t.GetProperty <bool>(Table.IsAutoGeneratedHistoryTable))
                        .Where(t => tables == null || !tables.Any() || tables.Contains($"[{t.Name.Parts[0]}].[{t.Name.Parts[1]}]"))
                        .Where(t => $"{t.Name.Parts[1]}" != HistoryRepository.DefaultTableName)
                        .ToList();

            foreach (var item in items)
            {
                var dbTable = new DatabaseTable
                {
                    Name   = item.Name.Parts[1],
                    Schema = item.Name.Parts[0],
                };

                if (item.MemoryOptimized)
                {
                    dbTable["SqlServer:MemoryOptimized"] = true;
                }

                GetColumns(item, dbTable, typeAliases, model.GetObjects <TSqlDefaultConstraint>(DacQueryScopes.UserDefined).ToList());
                GetPrimaryKey(item, dbTable);

                dbModel.Tables.Add(dbTable);
            }

            foreach (var item in items)
            {
                GetForeignKeys(item, dbModel);
                GetUniqueConstraints(item, dbModel);
                GetIndexes(item, dbModel);
            }

            return(dbModel);
        }