0

I developed Asp.net application with Entity Framework and now i need to change entity connection string at run time.I tried following way.

public class DataLayer(){

static DataLayer()
{
((EntityConnection)_dbEntity.Connection).StoreConnection.ConnectionString =    GetConnectionString();

//GetConnectonString() returns "user id=xxxx;password=xxxx;database=xxxx;server=xxx.xxx.xx.xx"
}

static DBContext _dbEntity = new DBContext();
//other codes
}

And I checked following links too.Still I couldn't change it.

http://msdn.microsoft.com/en-us/library/bb738533(v=vs.90).aspx

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/8a89a728-6c8d-4734-98cb-11b196ba11fd

2
  • Static context in ASP.NET application = failure. Commented Jul 16, 2012 at 8:12
  • i changed that one still i cant change it. Commented Jul 16, 2012 at 8:36

2 Answers 2

2

You cannot change connection string of existing context. If you want to control connection string at runtime you must pass connection string to DbContext or ObjectContext constructor.

Btw. as I already mentioned in comment - you must not use static context in ASP.NET. You should never use static context at all. Your application will not work correctly if you will continue with static context.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep i tried that one too. I got following error. "Keyword not supported: 'user id'." It means connection string is not supported for entityconnection.I tried above links in question too for that.
If you are using EDMX you must use valid entity connection string format. What you are passing is not entity connection string format.
0

Finally I got answer.I apply this dbcontext to a Entity datasource of a grid.So it create context at runtime it was fixed.So I changed that one.

 protected void EntityDataSource1_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
    {
        var dp=new DBContext();
        e.Context = dp;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.