C#C
C#3y ago
Core

EF Database is not seeded

Hello!
I have configured an Entity and the table needs to have an initial row.
After creating the migration and updating the database the row is still not there.
Any ideas what might be missing?

DbContext
c#
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
    }


Entity
c#
    public void Configure(EntityTypeBuilder<Domain> builder)
    {
        builder.HasKey(d => d.DomainId);

        builder.HasOne(d => d.User)
            .WithMany()
            .HasForeignKey(d => d.UserId)
            .IsRequired(false);

        builder.HasIndex(d => d.DomainName)
            .IsUnique();

        var sharedDefaultDomainName = new Domain
        {
            DomainId = Guid.NewGuid(),
            UserId = null,
            DomainName = "localhost:7151",
        };

        builder.HasData(sharedDefaultDomainName);   //Here
    }
Was this page helpful?