C
C#12mo 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());
}
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
}
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
}
11 Replies
Jimmacle
Jimmacle12mo ago
what's in your migration?
Core
CoreOP12mo ago
The whole database schema, but the seeded data is not there There is no error or anything, the data is simply not inserted into the Table
Jimmacle
Jimmacle12mo ago
if your migration is missing the appropriate SQL to seed the data then you didn't create a migration after you added it to be clear, i'm talking about the actual c# code that represents the migration not what your database looks like
Core
CoreOP12mo ago
I just deleted my whole Migration folder/database to be sure. Ran Add-Migration and Update-Database
Core
CoreOP12mo ago
Stumbled across this GitHub answer by @roji: https://github.com/npgsql/efcore.pg/issues/1588#issuecomment-733478755 and there is no difference in my code snippet
GitHub
HasData does not work with EntityTypeBuilder · Issue #1588 · npgsql...
.NET 5.0.0 .NET Command-line Tools 5.0.0 I can generate seed data in migrations using DbContext's ModelBuilder.HasData but when I use EntityBuilder.HasData, no seed data is generated.
Jimmacle
Jimmacle12mo ago
right but can you check the migration so we can see if it's even including your seed data in the migration file if it is a bug you'll need to provide that information anyway also, did you try the workaround mentioned in that issue?
Core
CoreOP12mo ago
I haven't, but it looks like that's the only option Yes this way it works, but now the Db Configuration is not modularized. With multiple seed it will become messy
Jimmacle
Jimmacle12mo ago
do you absolutely need to use migrations to seed your data? i do seed data independently when the application starts based on if the relevant tables are empty or not
Core
CoreOP12mo ago
Yes, it is critical to have this row in the db by default
Jimmacle
Jimmacle12mo ago
that's not what i asked
Core
CoreOP12mo ago
Sorry. I prefer seeding it with Migration, since it is not for testing purposes Anyway, thanks for the help! I ended up leaving it in the DbContext
Want results from more Discord servers?
Add your server