it:ad:code_first:migrations:howto:custom_operations

Entity Framework 6

Summary

 public class HelpSeederOperation : MigrationOperation
    {
        /// <summary>
        /// List of Help Entries to process
        /// </summary>
        public List<HelpEntry> HelpEntries { get; private set; }

        public HelpSeederOperation(List<HelpEntry> helpEntries)
            : base(null)
        {
            HelpEntries = helpEntries;
        }
         // Clean out the database
        public override bool IsDestructiveChange
        {
            get { return false; }
        }
    }


    public static class SeederExtensions
    {
        public static void AddHelpEntries(this DbMigration migration, List<HelpEntry> helpEntries)
        {
            ((IDbMigration)migration).AddOperation(new HelpSeederOperation(helpEntries));
        }
    }

    public class HomeGrownHelpSeedingSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
    {
        protected override void Generate(MigrationOperation migrationOperation)
        {
            var operation = migrationOperation as HelpSeederOperation;

            if (operation != null)
            {
                // Create the custom SQL based on the List of HelpEntry items sent through.
                var sb = new StringBuilder();
                sb.AppendLine("SET IDENTITY_INSERT [Xact_HelpEntry] ON");
                operation.HelpEntries.ForEach( x =>
                    {
                        sb.AppendLine(
                            string.Format(
                                "INSERT INTO [Xact_HelpEntry] VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}) ",
                                x.Id,
                                x.ParentFK,
                                x.Enabled,
                                x.Key,
                                x.KeyLowered,
                                x.ResourceFilter,
                                x.ResourceKey,
                                x.CategoryFK,
                                x.Description,
                                1,
                                0));
                    });
                


                Statement(sb.ToString());

            }
        }
        
        

  • /home/skysigal/public_html/data/pages/it/ad/code_first/migrations/howto/custom_operations.txt
  • Last modified: 2023/11/04 23:18
  • by 127.0.0.1