EF Core by Roman

Setting Database Schema Name

To set a default schema name for your database context in Entity Framework using C#, follow these steps:

  1. Open your DbContext class.
  2. Override the OnModelCreating method.
  3. Use modelBuilder.HasDefaultSchema("YourSchemaName") to configure the default schema.

Configure the default schema in OnModelCreating method

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure default schema
    modelBuilder.HasDefaultSchema("Sales");
}

This ensures that tables created by Entity Framework will use the specified schema (Sales in this case).

For further reference, you can check out the discussion on c# - Setting schema name for DbContext - Stack Overflow

Comments

Leave a Comment

All fields are required. Your email address will not be published.