none
Setting a single connection string on multiple EF 4.1 contexts RRS feed

  • Question

  • I have several contexts on a code-first EF4.1 approach, this is required by my modular architecture. I want contexts in all modules to use one single database so I have provided them all with one single connection string. The problem is I get this exception "The model backing the 'xxxContext' context has changed since the database was created" when the model builder tries to build the database. 
    You should also know that my contexts are not inter-connected so changes in one should not affect another.

    Friday, October 21, 2011 10:08 AM

Answers

  • Hi Vahid,

    This has to do with the fact it's trying to read the EdmMetadata table and it's missing or invalid.  Since you may be using an existing database (instead of letting EF 4.1 create it) or otherwise have a scenario where you don't need this table, you need to turn off using it.  This is a common problem that many experience when doing code first.  To fix , just add this code to your context constructor:

     
    public class MyContext: DbContext
    {
    public DbSet<MyEntity> MyEntities { get; set; }
    
    public MyContext(string connectionString): base(connectionString)
    {
           Database.SetInitializer<MyContext>(null);
    }
    
    .......
    
    }
    


     


    Tom Overton
    Friday, October 21, 2011 1:54 PM

All replies

  • Hi Vahid,

    This has to do with the fact it's trying to read the EdmMetadata table and it's missing or invalid.  Since you may be using an existing database (instead of letting EF 4.1 create it) or otherwise have a scenario where you don't need this table, you need to turn off using it.  This is a common problem that many experience when doing code first.  To fix , just add this code to your context constructor:

     
    public class MyContext: DbContext
    {
    public DbSet<MyEntity> MyEntities { get; set; }
    
    public MyContext(string connectionString): base(connectionString)
    {
           Database.SetInitializer<MyContext>(null);
    }
    
    .......
    
    }
    


     


    Tom Overton
    Friday, October 21, 2011 1:54 PM
  • Thanks Tom, but it leads to another problem. I'm doing it code-first, so I do want model builder to create my database objects. The solution you provided kicks model builder out so EF4.1 expects database tables to be there already. Can't I have model builder in action with multiple contexts?
    Saturday, October 22, 2011 7:32 AM
  • Hi,

    --------------------------------

    If you have not done any other configuration in your application, then calling the string constructor on DbContext with the database name you want to use will cause DbContext to run in Code First mode with a database connection created by convention to the database of that name. For example:

    public class UnicornsContext : DbContext
    {
        public UnicornsContext()
            : base("UnicornsDatabase")
        {
        }
    }

    Alternatively, you can use the form “name=<connection string name>” for the string passed to the DbContext constructor. For example:

    public class UnicornsContext : DbContext
    {
        public UnicornsContext()
            : base("name=UnicornsCEDatabase")
        {
        }
    }

    This form makes it explicit that you expect the connection string to be found in your config file. An exception will be thrown if a connection string with the given name is not found.

    http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

    -------------------------------

    Have a nice day

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 1, 2011 8:48 AM
    Moderator