matthewbaskey
matthewbaskey

Reputation: 1037

Entity Framework 5 : changing the database connection

I have a made an EntityFramework model based on my Demo Database.

I want to include connection strings for Staging and production and then in my Console App prompt the user to see which database they want to perform the operation on.

When I was prompted to setup the EF.edmx file I just chose to store the connection string in the app.config file. I have seen a link to change the Connection string of EntityFramework Context when initializing it here

However when I store another connection to my Staging Database, I get an error "Keyword not supported: 'metadata'"

So I stripped down the connection string not to include the EntityFramework type of parameters such as metadata=res://*/MyDBItems.csdl|res://*/MyDBItems.ssdl blah blah and used a very simple database connection string

data source=myDB;initial catalog=myDB;user id=myUser;password=myPass;MultipleActiveResultSets=True;

now my Context does instanciate but when I get to a query I get another error about:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

How can I switch between different databases with Entity Framework 5, thanks!

Upvotes: 1

Views: 1507

Answers (1)

matthewbaskey
matthewbaskey

Reputation: 1037

forget it I got it....what I did...I made a 2nd constructor in the Context.cs file

    public MyContext(string dbModel) : base("name="+dbModel) {

    }

then in my app.config I have the various settings for the Demo,Staging & Production database connections....this took the full entityframework connection string. I think the link I provided was for Code-First but I am using Database First.

Upvotes: 1

Related Questions