Setting Schema Name of Database in Application

Viewed 61

Is it possible to set the Schema of a DbContext from the application that is referencing an assembly?

This is what I have now:

services.AddDbContext<ManagementStudioDbContext>(options =>
                options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString),
                    optionsBuilder =>
                        optionsBuilder.MigrationsAssembly(assemblyName)
                )
            );

ManagementStudioDbContext has a different schema and I was hoping to set it so that any calls back to ManagementStudio will always have a schema.

1 Answers

You can specify schema in OnModelCreating method

 modelBuilder.Entity<Blog>().("User", schema: "acc");

Also you can use DataAnotation attribute

 [Table("User", Schema = "acc")]
 public class User{...}
Related