CreateDatabase() error

CreateDatabase() error

Postby DeepSeaDave » Tue 14 Dec 2010 20:32

I am trying to use the CreateDatabase() method. When I call it it throws an exception "Error on opening DbConnection.". The inner exception says "Unknown database 'mynewdatabase'". Now, if I am trying to create the database, one would expect it to not exist already!!!!! Any thoughts?
DeepSeaDave
 
Posts: 12
Joined: Wed 12 May 2010 16:48

Postby DeepSeaDave » Tue 14 Dec 2010 20:42

As a side note... If I call CreateDatabase(False, True) it creates the database that the datacontext connected to when I first created the Data Context using the Entity Developer. I get the error "Can't create database 'auction'; database exists"!

This did work before I upgaded to version 6 of dotConnect!
DeepSeaDave
 
Posts: 12
Joined: Wed 12 May 2010 16:48

Postby StanislavK » Wed 15 Dec 2010 18:03

Concerning the problems you've described:

1) As I can understand, the 'mynewdatabase' database is specified in the connection string. Am I correct? As there is no such database, an exception is thrown. It is necessary to connect to some other database to be able to execute the SQL creating 'mynewdatabase'.

E.g., you can connect to the default schema to create the new one:
Code: Select all
MyDataContext dc = new TestDbDataContext("Host = MyHost; User Id = root; password = root; Port =3306;");
dc.CreateDatabase(false,true);

2) The CreateDatabase behaviour was changed in the latest 6.0.58 version. In the CreateDatabase(bool ignoreErrors, bool createSchema) overload, the second argument specifies whether database itself should be created. To create database objects in the existing schema, please set this argument to false.

Feel free to contact us if you encounter any problems with this.
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Postby DeepSeaDave » Wed 15 Dec 2010 19:15

Yes, 'mynewdatabase' is the name of the database I'm trying to create. It was embedded in the connectionsting.

If I leave out the database name in the connectionstring then I get an error saying "No database selected" when I call CreateDatabase(). That's obvious-I can't create a database if I don't tell what the database name is! But if I put the databasename in the connectionstring I get the "Unknown database 'mynewdatabase'" error....

I need to get this to work. Assume I just installed a brand new copy of MySQL on a client's computer. There are no databases on it. I need to be able to start my application and have the datacontext.CreateDatabase() method create the schema!

PLEASE tell me what is going wrong here!

Thanks,
Dave
DeepSeaDave
 
Posts: 12
Joined: Wed 12 May 2010 16:48

Postby StanislavK » Thu 16 Dec 2010 12:27

Please try specifying an existing database in the connection string. E.g., if the server has just been installed, there should be at least 'mysql' and 'test' databases. The name of the database that should be created is specified in the model, thus it does not matter to what database you are connecting, as long as this database exists and the account you are using has privileges for creating databases.

I.e., assume that the model is created from the 'MyDatabase' schema on your development server, and you are using this model to create this schema on a new server that was just installed. You can perform the following for this purpose:
Code: Select all
MyDataContext dc = new MyDataContext("Host = MyHost; User Id = root; password = root; Port =3306; Database = mysql;");
dc.CreateDatabase(false,true);

In this sample, you are connecting to the 'mysql' database and creating 'MyDatabase' on the new server.

Please tell us if anything is unclear.
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Postby DeepSeaDave » Thu 13 Jan 2011 01:08

Ok. I get it. The "default" name of the database is embedded in the datacontext.mapping.databasename property. My problem has been all along this scenario: The mapping.databasename is "auction". When a customer is first installing this system I let them choose the name of the database they'd like to create. So in the connection string I pass in the database name "myauction". I really want to use the datacontext.createdatabase method to create the database with the name I provided, "myauction" and NOT create the database using the embedded "auction" name. How do I do this? I need to be able to do this!
DeepSeaDave
 
Posts: 12
Joined: Wed 12 May 2010 16:48

Postby DeepSeaDave » Thu 13 Jan 2011 01:43

I think there may be a bug. According to Microsoft
http://msdn.microsoft.com/en-us/library ... abase.aspx

The name of the database is derived by using the following algorithm:
*If a database is identified in the connection string, its name is used.
*If a DatabaseAttribute attribute is present, its Name property is used as the name of the database.
*If there is no database tag in the connection string and a strongly typed DataContext is used, a database that has the same name as the DataContext inheriting class is checked.
*If a weakly typed DataContext is used, an exception is thrown.
*If the DataContext has been created by using a file name, the database corresponding to that file name is created.

Therefore I should be able to provide a new database name in the connection string and CreateDatabase should create it!
DeepSeaDave
 
Posts: 12
Joined: Wed 12 May 2010 16:48

Postby StanislavK » Fri 14 Jan 2011 10:34

We will analyze the possibility of creating the database specified in the connection string, and inform you about the results here.

At the moment, the following workaround is available:
- connect without specifying a database or to an existing one;
- create a database with a user-specified name;
- set the new database name in the connection string of the DataContext instance;
- execute the DataContext.CreateDatabase() method (with the createSchema argument set to false).

This supposes that the database name is not used inside DataContext. To remove it, you can modify the .lqml file:
- clear the Name property of the Database node;
- remove database prefixes from the table names.
Otherwise, you can change the code generation template in the way described here:
http://www.devart.com/forums/viewtopic.php?t=17930
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Postby StanislavK » Fri 28 Jan 2011 13:38

We have fixed this issue, now it is possible to create a new MySQL database with the CreateDatabase method with the createSchema argument set to true. In this case, an attempt to connect is performed as if no database is specified in the connection string, and the database is created.

This fix is available in the new 6.10.96 build of dotConnect for MySQL. This build can be downloaded from
http://www.devart.com/dotconnect/mysql/download.html
(the trial version) or from Registered Users' Area (for users with active subscription only):
http://secure.devart.com/

For the detailed information about the fixes and improvements available in dotConnect for MySQL 6.10.96, please refer to
http://www.devart.com/forums/viewtopic.php?t=20119
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: CreateDatabase() error

Postby parshim@gmail.com » Fri 28 Jun 2013 05:59

Hi,

I am encounter the same issue with latest dotConnect for MySQL (7.6.226.0) issue working with EF5.

If schema doesn't exists I got Unknown database error when I call DbContext.Database.Create()

Please advice,

Michael.
parshim@gmail.com
 
Posts: 1
Joined: Thu 27 Jun 2013 13:59

Re: CreateDatabase() error

Postby Shalex » Wed 03 Jul 2013 13:28

Currently this is a designed behaviour: the database should be created manually (e.g.: via plain ADO.NET with MySqlCommand) before the first usage of the context. We are investigating the possibility of creating database via EF engine when the corresponding database initialization strategy is used. We will notify you about the result.
Shalex
Devart Team
 
Posts: 7391
Joined: Thu 14 Aug 2008 12:44

Re: CreateDatabase() error

Postby Shalex » Wed 10 Jul 2013 12:46

The Database value is added to the DeleteDatabaseBehaviour enumeration for creating/removing database when using CreateDatabase()/DropDatabase() functionality. We will notify you when the corresponding build of dotConnect for MySQL is available for download.
Shalex
Devart Team
 
Posts: 7391
Joined: Thu 14 Aug 2008 12:44

Re: CreateDatabase() error

Postby Shalex » Thu 18 Jul 2013 15:02

New version of dotConnect for MySQL 7.7 is released!
It can be downloaded from http://www.devart.com/dotconnect/mysql/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=2&t=27557.
Shalex
Devart Team
 
Posts: 7391
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for MySQL