首页 > 解决方案 > 无法附加文件:FirstMVC.Models.EmployeeContext.mdf' 作为数据库'FirstMVC.Models.EmployeeContext'

问题描述

我收到此错误:

无法将文件“FirstMVC.Models.EmployeeContext.mdf”附加为数据库“FirstMVC.Models.EmployeeContext”

源错误:

第 19 行:员工 employee = empContext.Employee.Single(emp => emp.EID == id);
第 21 行:返回视图(员工);

源文件:FirstMVC\Controllers\EmployeeController.cs

这是我的web.config

<connectionStrings>
    <add name ="EmpolyeeContext" 
         connectionString="Data source=.; Initial Catalog=MVCPractice1; integrated security=True;"
         providerName="System.Data.SqlClient"/>
</connectionStrings>

员工控制器:

public ActionResult Details(int id)
{
    EmployeeContext empContext = new EmployeeContext();
    Employee employee = empContext.Employee.Single(emp => emp.EID == id);
             
    return View(employee);
}

EmployeeContext.cs:

public class EmployeeContext : DbContext
{
    public DbSet<Employee> Employee { get; set; }
}

员工.cs:

[Table("tblEmployee")]
public class Employee
{
        [Key]
        public int EID { get; set; }
        public string EName { get; set; }
        public string EGender { get; set; }
        public string ECity { get; set; }
}

到目前为止我尝试了什么:

更改数据库名称并测试与 ADO.Net 实体数据模型的连接。

提前致谢。

标签: asp.net-mvcdatabaseentity-framework

解决方案


推荐阅读