Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change the Schema name

I´m developing an ASP.NET MVC app that uses EF 4.1 Code First.

I have to change the default schema name (dbo) to another name.

I tried this:

public string SchemaName;

public void MyContext()
{
    SchemaName = GetSchemaName();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Account>().ToTable("TB_ACC_HOLDERS", SchemaName);
}

But it´s not working. When I get a new instance of my Context and call some of my Tables.. the generated query still with "dbo" schema name.

Anyone have some idea to solve that?

like image 779
yanborowski Avatar asked Nov 21 '11 16:11

yanborowski


1 Answers

After some hours in debug mode, I get the error.

At the constructor, I get the schema name on connectionString and set the property (schemaName) whit the value.

But, when the execution get the OnModelCreating() method, my schemaName property was set, who knows why, to NULL.

Then I put the schemaName variable fixed on code at the method. When I do that everything are right.

Thanks everybody!

Here, the code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //It works fine
    var schemaName = "SYSTEM";
    modelBuilder.Entity<Account>().ToTable("TB_ACC_HOLDERS", schemaName);
}
like image 71
yanborowski Avatar answered Oct 06 '22 14:10

yanborowski