Open Closed

Create database from Blazor Server #4728


User avatar
0
desenvolvimento@logicpulse.com created

Hi,

We created our first application on Blazor Server, and published it for IIS, however when opening it for the first time, we found that the database was not created.

Is it necessary to always run DB Migrator? Is there no possibility of creating and updating the Database on the Blazor Server itself?

Thank's


5 Answer(s)
  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    You can call migration after the application starts. In your blazor-server module you can override OnPostApplicationInitializationAsync method:

    public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        await context.ServiceProvider
            .GetRequiredService<BookStoreDbMigrationService>() // Your applicationDbMigrationService
            .MigrateAsync();
    }
    
  • User Avatar
    0
    desenvolvimento@logicpulse.com created

    Hi,

    Thank's for the response.

    By doing this, the database is successfully created, however the following error is always displayed, and the server does not start. If we remove the OnPostApplicationInitializationAsync after database creation, it runs normally. In DBMigrator this error does not happen.

    An exception occurred in the database while saving changes for context type 'Volo.Abp.LanguageManagement.EntityFrameworkCore.LanguageManagementDbContext'. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'dbo.AbpLanguages' with unique index 'IX_AbpLanguages_CultureName'. The duplicate key value is (tr).

    DBMigrator

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    The duplicate key value is (tr).

    Have you changed the default LanguageInfo?

    Please share the AbpLanguagestable records.

    LanguageManagementDataSeeder will check for duplicates. Maybe you can Debug it to see.

  • User Avatar
    0
    desenvolvimento@logicpulse.com created

    No, the LanguageInfo is the default.

    I don't have the code you provided, but when debugging, I found the problem. The AbpLocalizationOptions is configure two times (BlazorModule and DomainModule). Can I ignore the BlazorModule?

    BlazorModule

    DomainModule

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Is it necessary to always run DB Migrator?

    You need to use Migrator to create a new database.

    https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations

    By doing this, the database is successfully created,

    Please undo this.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09