Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Seed Method doesn't run from the first time #1770


User avatar
0
moustafa created

hello i have a problem in seed method that it doesn't run all code inside from the first time so i have to run update-database -verbose several times to get all code executed

protected override void Seed(EntityFramework.UmbrellaDbContext context)
        {
            context.DisableAllFilters();

            context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('AbpUsers', RESEED, 101)");
            context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('HmPatients', RESEED, 10001)");
            context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('HmDoctors', RESEED, 201)");

            if (Tenant == null)
            {
                //Host seed
                new InitialHostDbBuilder(context).Create();

                //Default tenant seed (in host database).
                new DefaultTenantBuilder(context).Create();
                new TenantRoleAndUserBuilder(context, 1).Create();
            }
            else
            {
                //You can add seed for tenant databases using Tenant property...
            }

            new InitialRelationshipCreator(context).Create();
            new InitialClinicCreator(context).Create();
            new InitialAppointmentCreator(context).Create();
            context.SaveChanges();
        }

6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Seed code is executed by Entity Framework. Does it run partially or doesn't run at all in first try? This happened to us a few times but we also could not understand why (because it does not throw any exception). We suspect from a problem in EF.

    As you know AspNet Zero has a Migrate.exe tool. Is it working as expected in first run?

  • User Avatar
    0
    moustafa created

    actually it run from the first try but not all code get executed

    so i use this code to solve the problem

    private readonly bool _pendingMigrations;
            public Configuration()
            {
                AutomaticMigrationsEnabled = true;
                ContextKey = "ProjectName";
    
                // Check if there are migrations pending to run, this can happen if database doesn't exists or if there was any change in the schema
                var migrator = new DbMigrator(this);
                _pendingMigrations = migrator.GetPendingMigrations().Any();
    
                // If there are pending migrations run migrator.Update() to create/update the database then run the Seed() method to populate
                //  the data if necessary
                if (_pendingMigrations)
                {
                    migrator.Update();
                    Seed(new EntityFramework.ProjectNameDbContext());
                }
            }
    
    protected override void Seed(EntityFramework.ProjectNameDbContext context)
            {
                context.DisableAllFilters();
    
                /////////////////// rest of code ////////////////
    
                context.SaveChanges();
                base.Seed(context);
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I also had this problem a few times but I couldn't catch it when I want to. Do you know how to reproduce this problem ? Or does it happen randomly ?

  • User Avatar
    0
    moustafa created

    Actually it occurs often please refer to the previous post of mine try the code and it will fix the problem let me know please of your progress by the way if you want to debug seed method effects you can use this code in seed method

    // Debugging Package Manager Console Update-Database Seed Method
                if (System.Diagnostics.Debugger.IsAttached == false)
                   System.Diagnostics.Debugger.Launch();
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    After applying codes for this issue <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/issues/367">https://github.com/aspnetzero/aspnet-zero/issues/367</a>, Migration problem is not happening for me anymore. Can you check that ?

  • User Avatar
    0
    moustafa created

    great news :D thank you