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

7
  • Does this answer your question? what's the issue with AttachDbFilename Just don't use it, it has issues. Instead attach your DB properly Commented Aug 3, 2021 at 2: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. Commented Aug 3, 2021 at 2:33
  • 2
    Probably the previous connection is still open, so the file is being held open and therefore preventing attaching. Commented Aug 3, 2021 at 2:54
  • If you go and find the MDF file and try renaming it, what happens? As already mentioned, its probably still attached / open Commented Aug 3, 2021 at 3:18
  • I've tried renaming, deleting completely, placing the .mdf file into different drivers and folders - no change. The problem is still the same. Commented Aug 3, 2021 at 15:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.