Base solution for your next web application
Open Closed

Seed data via migration #8676


User avatar
0
-bitman created

Hello ANZ Team,

I am trying to seed my table using migration and it is working well when applying the migration across multiple tenants with their own separate database. The issue I have though is that the TenantId column is NULL in the separate database. I would like to ask how I can resolve this issue since I cannot see in the migrationBuilder class a property to get the TenantId when the Migrator runs it.

Please advise.

Thank you!


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi ramilcatalandomingo

    Please share your migration code.

    Related discussions before https://support.aspnetzero.com/QA/Questions/8443#answer-0e26291f-49c7-4ccb-4ea2-39f32cd75cfe

  • User Avatar
    0
    -bitman created

    Hello @malimig,

    I tried to implement the one in the related discussions but I also wanted to see if it is achievable via migration so I can also use the Migrator tool and have a single codebase. Anyway here is the migration file content as requested.

    using Microsoft.EntityFrameworkCore.Migrations;
    
    namespace LogDesk.Migrations
    {
        public partial class Seed_Countries_Entity : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.Sql("INSERT INTO [dbo].[Countries] ([Code], [Description], [IsEuro], [CurrencyCode], [CurrencyDescription], [CurrencySymbol], [Inactive], [CreationTime], [IsDeleted]) VALUES ('GB', 'United Kingdom', 0, 'GBP', 'United Kingdom Pounds', '£', 0, GETDATE(), 0)");
                migrationBuilder.Sql("INSERT INTO [dbo].[Countries] ([Code], [Description], [IsEuro], [CurrencyCode], [CurrencyDescription], [CurrencySymbol], [Inactive], [CreationTime], [IsDeleted]) VALUES ('US', 'United States of America', 0, 'USD', 'United States Dollars', '$', 0, GETDATE(), 0)");
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.Sql("DELETE FROM [dbo].[Countries]");
            }
        }
    }
    

    Notice that I am not assigning a TenantId value to the SQL statement since I don't know how to get it when applying the migration across the tenants.

  • User Avatar
    0
    maliming created
    Support Team

    I suggest you use code to perform the migration instead of inheriting the Migration class.

  • User Avatar
    0
    -bitman created

    Thanks for the suggestion @maliming