MySQL (ADO.NET) Giving 'No Database Selected' Error when database is indeed selected


Using ADO.NET with the MySql Connector (MySql.Data) driver, I open a new, non-pooled query (no initial catalog/database is specified in connection string) and issue the following query:

ALTER TABLE `public`.`table2` 
ADD CONSTRAINT `fk_table2_table1`
FOREIGN KEY (`table1_id`) REFERENCES `table1` (`id`);

The structure of the two tables is as follows:

CREATE TABLE `public`.`table1`
    id int primary key

CREATE TABLE `public`.`table2`
    id int primary key,
    table1_id int

The ALTER TABLE query causes the following exception:

MySql.Data.MySqlClient.MySqlException (0x80004005): No database selected

However, with the same setup, if I run the ALTER TABLE query using the mysql command line tool (and several other MySQL clients) I get no such error.

Does anyone have suggestions to things I can look into. I'm currently thinking this is an issue with the MySql Connector (MySql.Data) driver, however, I could easily be wrong here.


We are not able to reproduce this with a minimally viable repro. It is likely, the issue lies somewhere in our own code and not in the driver.

Another update: We were actually able to successfully reproduce this using just the mysql command line tool. It requires Mysql 5.6.34 (won't repro in many other versions I tested including 5.6.41). Also, we reproduced in AWS RDS but not sure if that is required. In order to reproduce perform the following steps:

1) Fresh database install 2) login via mysql command line tool 3) create two tables

create schema public; create table public.table1( id int primary key); create table public.table2(id int primary key, table1_id int); alter table public.table2 add foreign key fk_name (table1_id) REFERENCES table1(id);

The final command results in the error:

ERROR 1046 (3D000): No database selected

asked on Stack Overflow Mar 12, 2020 by Adam Kamor • edited Mar 12, 2020 by Adam Kamor

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0