2

We have a web application based on Entity Framework database-first. We deploy that to a couple of enviornments. Not all of them store their data into the same schema name. I can change the database name in the web.config, but the schema name seems to be hard-coded in the EDMX.

Is there any way to get this fixed? My fruitless googling efforts leave me a bit frightened, and yet I cannot believe that the only way to deploy cleanly is to find-replace all occurrences of the schema name every time before we install it.

chiccodoro
  • 14,407
  • 19
  • 87
  • 130

1 Answers1

0

You can do something like this: In web.config:

<add key="DbSchema" value="dbo" />

Then on EntitiesDb class (assuming you use this name):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        string schema = WebConfigurationManager.AppSettings["DbSchema"]; //ServiceSettingsDictionary.GetSchemaName();
        if (!string.IsNullOrEmpty(schema))
        {
            modelBuilder.HasDefaultSchema(schema);
            //modelBuilder.Entity<ConnectionItem>().ToTable("ConnectionItems", schema);
        }
        base.OnModelCreating(modelBuilder);
    }e
danpop
  • 967
  • 13
  • 25
  • Thank you. Unfortunately, "once you use EDMX OnModelCreating is never called", according to http://stackoverflow.com/a/7876808/55787 – chiccodoro Mar 05 '15 at 11:40