-2

My application is based on entity framework 5, and the connection string is dynamically generated and used in the application. This is working fine. The only problem is if I do not put connection string in app.config file, then it gives an error. That the app.config file should contain the connection string. Is there any way, I can make my process not to find connection string in app.config file. The work around is I can put dummy connection string, but i want it should not look in app.config file for connection string. Please help. !!

Thanks in advance..

Hitesh
  • 3,508
  • 1
  • 18
  • 24
  • In which context do you use your connection string? Is the use automated or are you specifying the use yourself? – Flipbed May 14 '13 at 10:54

2 Answers2

2

An application does not look at connection strings, it's libraries you use that do that. Luckily your question is tagged with entity-framework, so I guess you somewhere just instantiate a new DbContext(). It would be nice if you could show on what line of code the error occurs.

When you search the web for "entity framework dbcontext pass connection string" and you will find this question which links to the manual somewhere:

public DbContext(string nameOrConnectionString)

So, just supply your valid connection string to the constructor when instantiating your Entity Framework context (not its name, as that again will make it look in the application's configuration).

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Thanks CodeCaster for your reply.

I am doing in this way. I have created one class which contains the static method, and in each DAL class i just call this method, and the instance for context is created with the passed connection string.

public static myDBEntities getDBContext(String connectionString) {   

        myDBEntities DB = new myDBEntities();

        DB.Database.Connection.ConnectionString = connectionString;

        return DB;
    }

My application works fine. There is no issue. I mean it takes the dynamically assigned connection string, But only problem is if I remove the connection string from app.config file, then it gives me error that it expects connection string in app.config. So is there any settings or something, which can lead EDMX not to find connection string in app.config.

Your help will be appreciated. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24