Change schema name in Entity Framework Core

October 18, 2022 by Anuraj

dotnet dotnetcore efcore

This post is about how to change schema name in EF Core. By default when we are running EF Core migrations, EF Core will create tables in the default dbo schema. We can change it with fluent API and using attributes.

In the fluent API, we can configure the schema with HasDefaultSchema method, which will apply the schema for all the tables. And if you want only for specific tables, we can use the ToTable method overload. Here is pseudo code.

public class SocialDbContext : DbContext
{
    public SocialDbContext(DbContextOptions options) : base(options)
    {
    }

    protected SocialDbContext()
    {
    } 
    public DbSet<Profile> Profiles { get; set; } = default!;
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("Social");    //To all the tables.
        modelBuilder.Entity<Profile>().ToTable("Profiles", "Social");   //To this specific table.
    }
}

And here is the implementation using the ToTable data annotations attributes.

[Table("Profile",Schema = "Social")]
public class Profile
{
    public int Id { get; set; }
    [Required, StringLength(100)]
    public string? FirstName { get; set; }
    [Required, StringLength(100)]
    public string? LastName { get; set; }
    [Required, StringLength(256)]
    public string? Email { get; set; }
    [StringLength(256)]
    public string? PictureUrl { get; set; }
    public DateTime CreatedOn { get; set; } = DateTime.UtcNow;
}

Using these two ways we will be able to customize the EF Core database schema.

Happy Programming.

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub