본문 바로가기

DB/Entity Framework

System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file [LocalDB파일 경로] failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.



LocalDB 파일로 개발하는 도중 만날 있는 예외 메시지입니다.

 

제가 만난 경우는 메인프로젝트의 App_Data 폴더에 xxxx.mdf local DB 있고 유닛테스트 프로젝트에서는 참조해서 쓰는 경우에 발생했습니다.

 

메인 프로젝트의 Web.config Connection String 섹션명과 유닛테스트 프로젝트의 App.config Connection String 섹션 명이 같게 설정되어 습니다.

 


관련 내용 검색은

 

1. localDB data source 

http://stackoverflow.com/questions/21563940/how-to-connect-to-localdb-in-visual-studio-server-explorer


2. UNC share 관련

 http://forums.asp.net/t/1956317.aspx?A+database+with+the+same+name+exists+or+specified+file+cannot+be+opened+or+it+is+located+on+UNC+share+



포스트를 참고했으나 해결책이 되진 못했습니다.

해결책은 연결 방식의 문제가 아니었고 localDb 참조하는 폴더 규칙이었습니다.

 

예로 Web.config LocalDB 참조 경로는

data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\xxxxx.mdf;


이고 유닛 테스트 프로젝트의 App.config LocalDB 참조 경로는

data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\ xxxxx.mdf;

 


App.config에는 


Server Explorer > Data Connections > 실제 연결된 접속 > Properties > Misc > Primary File Path 의 경로를 붙여 넣어 줍니다.