0

I am trying to create data model from the existing database by executing Scaffold-DbContext command at Package Manager Console by using this command:

Scaffold-DbContext "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\*****\Books.mdf;Integrated Security=True;Connect Timeout=30" "Microsoft.EntityFrameworkCore.SqlServer" -o Models

However, I get this error message which doesn't make sense to me since I point to the exact location where this .mdf file resides (I've searched the possible solutions to this error message). What else could be wrong?

The settings: Win10, VS2019, C#, Project: Console App (.NET Framework) and Console Application (I've tried both). NuGet packages installed: Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.Tools

The complete output:

> PM> Scaffold-DbContext "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\*****\Books.mdf;Integrated Security=True;Connect Timeout=30" "Microsoft.EntityFrameworkCore.SqlServer" -o Models

> Build started...
> Build succeeded.

>Microsoft.Data.SqlClient.SqlException (0x80131904):

An attempt to attach an auto-named database for file D:\*****\Books.mdf failed.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


ClientConnectionId:a64a8f06-79ad-4c0c-a08a-e45182b5787c
Error Number:15350,State:1,Class:14
An attempt to attach an auto-named database for file D:\*****\Books.mdf failed.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
PM> 

database

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TheGoogles
  • 15
  • 4
  • Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) Just don't use it, it has issues. Instead attach your DB properly – Charlieface Aug 03 '21 at 02:29
  • My Books2 database had no problems with such method when I was working with another project and it's an exact copy. However, that was seven months ago. I wonder if something has changed in the VS updates since that time. – TheGoogles Aug 03 '21 at 02:33
  • 2
    Probably the previous connection is still open, so the file is being held open and therefore preventing attaching. – Charlieface Aug 03 '21 at 02:54
  • If you go and find the MDF file and try renaming it, what happens? As already mentioned, its probably still attached / open – Nick.Mc Aug 03 '21 at 03:18
  • I've tried renaming, deleting completely, placing the .mdf file into different drivers and folders - no change. The problem is still the same. – TheGoogles Aug 03 '21 at 15:18
  • Your issue should be similar to the one described in this [post](https://stackoverflow.com/questions/28618257/how-to-avoid-recreate-an-existing-database-using-automatic-code-first-migration) – Dimi Takis Aug 03 '21 at 15:38
  • O.K. I got it working. Before I deleted the database I right-clicked it and chose Close the connection. Then I created a new connection and tested it. Then I created the new Console Application project and chose the latest Target Framework (5.0). Then I installed the NuGet packages and selected versions 5.0.0 for all of them. After that, I used the Scaffold-DbContext command and it worked. Thanks Charlieface and Nick.McDermaid. – TheGoogles Aug 03 '21 at 15:48

0 Answers0