SQL Server - Database 'Database' does not

2019-03-02 02:42发布

问题:

I'm trying to generate my database tables from my Entity Framework model, but I'm getting this error when I execute the generation query:

Database 'Database' does not exist. Make sure that the name is entered correctly.

I'm able to connect to the local server just fine.

My connection string, which was generated by VS when I selected 'Database.mdf':

metadata=res://*/Models.Models.csdl|res://*/Models.Models.ssdl|res://*/Models.Models.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

The database definitely exists, but I've noticed that the only databases it's picking up on the server are called master, model, msdb and tempdb.

Can anyone help?

Edit: I've just realised that the SQL connection dialog that comes up when you click "Execute SQL" allows you to connect to a server. But my DB isn't on a server, it's just a user instance database. But that's the only option, so how would I execute against my database? I have the database open in the 'Server Explorer'.

回答1:

The connection string is referring to a user instance database is that what you intended? I got the impression possibly not from the question.

If not try changing it to Data Source=ServerName\InstanceName;Initial Catalog=Database;Integrated Security=True;



回答2:

If you are using a user instance database, specify the full path to the location of the database with USE directive. For example:

USE [C:\Project\Database.mdf]
GO


回答3:

When connecting the server, you need to select another data source, then enter the name of the local server on which the database is located.



回答4:

I can't speak for Entity Framework, but within SQL, "database" is a (very) reserved word. Try calling your database (and it's files) something else, even if just "MyDatabase".