Entity Framework Core Issue - Update-Database Creating "User" Schemas

asp.net asp.net-core entity-framework entity-framework-core sql

Question

I've been working for quite a while on a production application using .NET Core 2.0 (originally 1.0) & EF Core.

This is a Code-First designed application, so all database changes have been made via Migrations.

Up until a recent deployment, Update-Database would make the changes correctly - however, recently it has been creating new schemas each time a model is added (instead of adding to the dbo schema like normal).

Example:

New Class Model: Test.cs
Table created in Database: DOMAIN\CurrentUser.Test

Any ideas on why this would be happening?

I suppose I can dig deep into the migrationBuilder to figure out how it is making Schema decisions, but seems like something simple must have changed to change the behavior.

"DOMAIN\CurrentUser" has dbo rights on the server in question.

1
2
12/10/2018 6:20:40 PM

Popular Answer

For changing schema from EF Core, try to configure with builder.HasDefaultSchema("newschema"); or builder.Entity<User>().ToTable.

For changing the schema for all tables.

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public DbSet<User> User { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema("newschema");
    }
}

For chaning the schema for specific table.

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public DbSet<User> User { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<User>().ToTable("User", "newSchema");
    }
}
0
12/12/2018 6:39:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow