Migration Not Working

Hello Radzen Team!

After setting up security and expanding its tables, I tried making the initial tables of my app. Followed the same process but got different results.

This is the result of "dotnet ef migrations add InitialTables -c SiSawContext".

I find it weird it was created in a new Migrations folder, outside of the data folder.

The only tables that are being created/updated are the Identity ones. So how can I create the initial tables migration for the other tables?

Hi @kim,

This question seems unrelated to Radzen and we can't provide assistance for it. I recommend looking online for Entity Framework Core Migrations related resources.

Hi @kim,
I'm facing the same problem and managed to solve it.

Why the only tables that are being created/updated are the Identity ones? this is because the "Database.Migrate" only call for the "identityDbContext", you can see it at the lower part of Startup.cs code:

        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapControllers();
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });

        //migration only call for this context
        identityDbContext.Database.Migrate();

        OnConfigure(app, env);

So you should call migration for your datasource context manually by extend the partial class onConfigure(app, env).

Below are steps that are working for me:

  1. create code first migration by targeting specific context and specific output directory. why? because there are more than one context, so we should told migration which one should be created and to what directory. below is the add migration command example:

    PM> add-migration CreateIndotalentTodo -context IndotalentTodoContext -outputdir Data\Migrations

  2. Create Startup.Custom.cs to extend the OnConfigure(app, env). You can add your Database.Migrate() script at this OnConfigure(app, env) partial method:

     partial void OnConfigure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         var indotalentConnectionString = Configuration.GetConnectionString("IndotalentTodoConnection");
    
         var options = new DbContextOptionsBuilder<IndotalentTodoContext>()
             .UseSqlServer(indotalentConnectionString).Options;
    
         using (var ctx = new IndotalentTodoContext(options))
         {
             ctx.Database.Migrate();
         }
     }
    

Best regards,
Ismail

1 Like

Thanks for your detailed solution. Even though I fixed my problem long ago, I'm sure this will help other forum members!