Programatically switch database connection string?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
DocDweeb
Posts: 4
Joined: Sat 04 Dec 2010 22:48

Programatically switch database connection string?

Post by DocDweeb » Tue 25 Jan 2011 03:52

I have the same database schema on several physical MySql databases, because my host has a 1GB limit on each one.

I'd like to use the same Linq Entity Model (lqml) for each physical database, and just switch the database connection based on the user.

I have tried changing the connection string using MyDataContext context = new MyDataContext("new connection string");

I have tried interrupting the creation of the data context with extension methods on OnCreated(), by changing Connection.ConnectionString = "new connection string";

In both cases I get an error reading from the database, such as "SELECT command denied to user .....". I believe the Database name is embedded in the model, and so even though I changed the connection string it is still trying to access the original database name.

Is there something I can do different, to allow me to change databases but use the same model? Maybe a parameter in the model itself?

Thanks!
-Doc

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Wed 26 Jan 2011 16:43

LinqConnect models keep schema names for handling the case of working with objects from different schemas in a single model.

To change this behaviour, you can clear the 'Name' parameter of the 'Database' node in the .lqml file, and re-generate the code.

Or, you can modify the code generation template so that schema name is never included into table mappings. For the information on how to do this, please refer to
http://www.devart.com/forums/viewtopic.php?t=17930

DocDweeb
Posts: 4
Joined: Sat 04 Dec 2010 22:48

It works!

Post by DocDweeb » Wed 26 Jan 2011 22:58

Thanks for the help!
I actually went to Model Explorer in Visual Studio, selected each of my entities, and changed the 'Source' property. It was generated as "schemaname.Tablename" and I changed it to just "Tablename".

I'll probably do the Template change too, as you suggest, so that future entities will already be generated without the schema in the entity definition.

Thanks again, and thanks for the quick response.
-Doc

EeeeEfff
Posts: 15
Joined: Wed 26 Oct 2011 00:48

Post by EeeeEfff » Wed 26 Oct 2011 04:50

Hi - I am having a similar problem, except I need to be able to allow different logins/passwords. I have removed the entire ConnectionString attribute from the lqml file and regenerated the code. The generated DataContext code reflects the change in that the connection string does not exist. But here's the problem: no matter what I provide the data context constructor as the connection string, I still get a successful connection. Is this information cached somewhere?

Any help would be much appreciated.

Thanks,
E.

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Tue 01 Nov 2011 07:11


Post Reply