Asp.Net Core 3.1 Seed data with conditionally

tcetin

I don't want to seed if the data exist in the modelBuilder.

Here is I tried and seeded again with different role ids so my user with the roles has gone.

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<IdentityRole>().HasData(
            new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() },
            new IdentityRole { Name = "PoUser", NormalizedName = "PoUser".ToUpper() },
            new IdentityRole { Name = "User", NormalizedName = "User".ToUpper() }
        );

    }

So how can I prevent data seeding with conditional rendering in Asp.Net Core 3.1 if data exist in the db ?

Thanks

OO7

It seems you must add a primary key into the model.

builder.Entity<IdentityRole>().HasData(
        new IdentityRole { Id = 1, Name = "Admin", NormalizedName = "Admin".ToUpper() },
        new IdentityRole { Id = 2, Name = "PoUser", NormalizedName = "PoUser".ToUpper() },
        new IdentityRole { Id = 3, Name = "User", NormalizedName = "User".ToUpper() }
    );

Because it will be used to detect data changes between migrations.

About Data Seeding.

Hope this could helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding Admins using Seed data Asp.net core

Seed test data for every test in ASP.NET Core / EF Core / xUnit.NET integration tests

ASP.NET Core 2 Seed Database

How to seed NetTopologySuite.Geometries.Point data from a Json file in ASP.Net core

Data Seed failed in hosted project in linux server. Asp .net core 2.1

Seed initial data in Entity Framework 7 RC 1 and ASP.NET MVC 6

Seed database AspNetUserRoles table in ASP.NET Core 3.1 MVC

Seed database user and role table in asp.net core 2

ASP.NET core Identity seed only creates the last user

Entity Framework core seed without asp.net app

Unable to seed data in ASP.NET Core in a static method due to exception 'A second operation started on this context before a previous'

.Net Core Identity seed data: unable to login using credentials seeded

Conditionally render disabled button in asp.net core view

asp.net core tag helper for conditionally add class to an element

Entity Framework Core seed data

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

How do I seed user roles in Identity ASP.NET Core 2.1 (Identity Scaffolded)

Data binding in ASP.NET Core

ChartJS shows chart but no data in ASP net core

asp net core database initialization (seeding the data)

ASP.NET Core Redirect with data object

Data annotations not working in ASP.NET Core

Filter authorized data in ASP.NET Core

Asp.Net Core class not passing data

Cannot serialize stream data asp .net core

How to get only 1 column data in JsonResult response in ASP.NET Core 3.1

asp.net core 3 routing actionmethod

ASP.NET core 3 ActionFilter and Viewbag

Streaming videos with ASP.NET Core 3