EF Core 6 Question
-
Is it possible to insert default data in a table that was created by Entity Framework? Here's my DBContext
public class SqlDataContext : DbContext
{
private string _connectionString = "";public DbSet<UserEntity> Users { get; set; } public SqlDataContext() { // This will be moved to the controller later on \_connectionString = @"Server=MAROIS\_KEVIN\_1\\SQLEXPRESS;Database=Test1;Trusted\_Connection=true;Encrypt=false;"; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(\_connectionString, options => options.EnableRetryOnFailure()); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserEntity>(entity => { /\* Users \*/ entity.ToTable("Users", "dbo"); entity.HasKey(e => e.UserId) .HasName("PrimaryKey\_UserId"); }); }
}
I would like to insert a default user into the User's table each time I create the database. Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Is it possible to insert default data in a table that was created by Entity Framework? Here's my DBContext
public class SqlDataContext : DbContext
{
private string _connectionString = "";public DbSet<UserEntity> Users { get; set; } public SqlDataContext() { // This will be moved to the controller later on \_connectionString = @"Server=MAROIS\_KEVIN\_1\\SQLEXPRESS;Database=Test1;Trusted\_Connection=true;Encrypt=false;"; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(\_connectionString, options => options.EnableRetryOnFailure()); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserEntity>(entity => { /\* Users \*/ entity.ToTable("Users", "dbo"); entity.HasKey(e => e.UserId) .HasName("PrimaryKey\_UserId"); }); }
}
I would like to insert a default user into the User's table each time I create the database. Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Data Seeding - EF Core | Microsoft Learn[^]
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Data Seeding - EF Core | Microsoft Learn[^]
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakAwesome, thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.