How to Change the Schema name

≯℡__Kan透↙ 提交于 2019-12-12 10:39:54

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/8215400/how-to-change-the-schema-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!