Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. EF Core 6 Question

EF Core 6 Question

Scheduled Pinned Locked Moved Database
questionasp-netdatabasesysadminhelp
3 Posts 2 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • K Kevin Marois

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • D 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 Kreskowiak

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Awesome, thanks!

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups