Can I change the default schema name in entity framework 4.3 code-first?

For those using Entity Framework 6, just use the HasDefaultSchema method:

public class Contexto : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("MyDefaultDbSchema");
    }
}

For database-first implementations, it's easy. Open the edmx file, right click -> Properties and set the default database schema.

For code-first, this article seems most promising: https://web.archive.org/web/20150210181840/http://devproconnections.com/entity-framework/working-schema-names-entity-framework-code-first-design


You could use the ToTable method to specify the schema name. If you do not specify the schema name, EF will by convention use dbo.

public class MyContext
{
    private string schemaName = "Foo";

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
    } 
}

In EF Code first, by default, everything is set up based on user access with a managerial access "DBO-Schema" in the SQL Server. But if a specific user is defined to work with a database that is common in shared hosting, then there will no longer be Dbo management access. This time the names of our tables are dbo.tableName, for example, someUser.tableName, and inaccuracy of this point makes it impossible to run the program. To modify and explicitly assign a user connected to a database. If you use metadata, the following method should be used:

[Table("MyTableName", Schema="MySchemaName")]
public class MyClassName
{
 //Other Lines...
}

Or (Whether or not Fluent API is customizable as follows:)

modelBuilder.Entity<Blog>().ToTable("MyTableName", schemaName:"MySchemaName");

Notice the following: enter image description here

a good reference for study: http://www.c-sharpcorner.com/article/fluent-api-in-code-first-approach/