Ejemplo n.º 1
0
        private LookupDbModel BuildModelFromContext(DbContext context)
        {
            // recurse through dbsets and references finding anything that uses an enum
            var enumReferences = FindEnumReferences(context);

            // for the list of enums generate and missing tables
            var enums = enumReferences
                        .Select(r => r.EnumType)
                        .Distinct()
                        .OrderBy(r => r.Name)
                        .ToList();

            var lookups = (
                from enm in enums
                select new LookupData
            {
                Name = enm.Name,
                NumericType = enm.GetEnumUnderlyingType(),
                Values = _enumParser.GetLookupValues(enm),
            }).ToList();

            var model = new LookupDbModel
            {
                Lookups    = lookups,
                References = enumReferences,
            };

            return(model);
        }
Ejemplo n.º 2
0
        private string BuildSql(LookupDbModel model)
        {
            var sql = new StringBuilder();

            sql.AppendLine("set nocount on;");
            if (UseTransaction)
            {
                sql.AppendLine("set xact_abort on; -- rollback on error");
                sql.AppendLine("begin tran;");
            }
            sql.AppendLine(CreateTables(model.Lookups));
            sql.AppendLine(PopulateLookups(model.Lookups));
            sql.AppendLine(AddForeignKeys(model.References));
            if (UseTransaction)
            {
                sql.AppendLine("commit;");
            }
            return(sql.ToString());
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Generates the migration SQL needed to update the database to match
 /// the enums in the current model.
 /// </summary>
 /// <param name="model">Details of lookups and foreign keys to add/update</param>
 /// <returns>The generated SQL script</returns>
 public string GenerateMigrationSql(LookupDbModel model)
 {
     return(BuildSql(model));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Make the required changes to the database.
        /// </summary>
        /// <param name="model">Details of lookups and foreign keys to add/update</param>
        /// <param name="runSql">A callback providing a means to execute sql against the
        /// server. (Or possibly write it to a file for later use.</param>
        public void Apply(LookupDbModel model, Action <string, IEnumerable <SqlParameter> > runSql)
        {
            var sql = BuildSql(model);

            runSql(sql, null);
        }