2
votes

I'm new to MVC technology, going though a training material and struck up with below error message with Entity Framework.

Here I'm trying to get one record from SQL Server Compact database but error occurs in EmployeeController class at step:

Employee employee = empContext.Emp.Single(x => x.EmployeeID == id);

Error message:

Cannot attach the file 'E:\DotNet\MVC4Application\MVC4Application\App_Data\MVC4Application.Models.EmployeeContext.mdf' as database 'MVC4Application.Models.EmployeeContext'.

Exception Details: System.Data.SqlClient.SqlException: Cannot attach the file 'E:\DotNet\MVC4Application\MVC4Application\App_Data\MVC4Application.Models.EmployeeContext.mdf' as database 'MVC4Application.Models.EmployeeContext'.

Connection string:

<connectionStrings>
    <add name="EmployeeContext" 
         connectionString="Data Source=E:\DotNet\MVC4Application\MVC4Application\App_Data\Database1.sdf;Persist Security Info=False"/>
</connectionStrings>

Could someone help me with this error message, please?

1
Your connection string shows a .sdf file (SQL Server Compact Edition) being referenced - just a typo ?? So what do you really want - attach an .mdf (SQL Server - full version) or use SQL Server CE (with a .sdf file)?? - marc_s

1 Answers

3
votes

You can try the following (one by one):

  • Deleting DB in SSMS
  • Kill IIS
  • Run the following commands via the package manager console:

    sqllocaldb.exe stop v11.0 (or 12.0?) 
    sqllocaldb.exe delete v11.0