连接到数据库时出现错误
我是Entity Framework的新手,在与数据库建立连接时出现此错误。连接到数据库时出现错误
我的模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCTest.Models
{
[Table("Employee")]
public class EmployeeModel
{
public int EmployeeId { get; set; }
public string name {get;set;}
}
}
我的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTest.Models;
namespace MVCTest.Controllers
{
public class Employee1Controller : Controller
{
// GET: Employee1
public ActionResult Details(int id)
{
EmployeeContext employeecontext = new EmployeeContext();
EmployeeModel employee= employeecontext.Employees.Single(emp => emp.EmployeeId == id);
return View(employee);
}
}
}
我EmployeeContext
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
namespace MVCTest.Models
{
public class EmployeeContext:DbContext
{
public DbSet<EmployeeModel> Employees { get; set; }
}
}
,我想连接到我的本地计算机和数据库名称我的连接字符串TMA:
<connectionStrings>
<add name="EmployeeContext"
connectionString="server=.;Database=TMA;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
但每当我运行这段代码我收到此错误
“上的环境不能被创建的模型,同时使用。如果在 OnModelCreating方法中使用上下文,或者同时由多个线程访问同一上下文实例,则可能会抛出此 异常。需要注意的DbContext 及相关类中的实例成员不能保证是线程安全的。”
谁能请提供有关这个任何帮助吗?
非常感谢。
基于错误消息,这听起来像context
不能用于这样的动作方法 - 也许将它移动到像大多数例子那样的类作用域会做伎俩吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTest.Models;
namespace MVCTest.Controllers
{
public class Employee1Controller : Controller
{
private readonly EmployeeContext db;
public Employee1Controller()
{
db = new EmployeeContext();
}
// GET: Employee1
public ActionResult Details(int id)
{
EmployeeModel employee = db.Employees.Single(emp => emp.EmployeeId == id);
return View(employee);
}
}
}
“如果您未指定连接字符串或显式的名称,实体框架假定连接字符串名称与类名称相同。“所以你说的不是真的 –
@AlexanderDerck,谢谢你......我不知道。 –
它可能有点混乱:-) –
试试这个
<connectionStrings>
<add name="EmployeeEntities" connectionString="Data Source=.;Initial Catalog=Employee;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
的EntityContext类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
namespace MVCTest.Models
{
public class EntityContext:DbContext
{
public EntityContext()
: base("EmployeeEntities")
{
//#if DEBUG
Database.SetInitializer<EntityContext>(new DatabaseInitializer());
var ensureDLLIsCopied =
System.Data.Entity.SqlServer.SqlProviderServices.Instance;
//#endif
}
public DbSet<EmployeeModel> Employees { get; set; }
}
}
'服务器=;'应该是问题,如果你想使用的LocalDB将其更改为'(的LocalDB)\ 11.0;'[参考](https://msdn.microsoft.com/en-us/library/jj653752(v = vs.110).aspx) –
您使用普通SQL Server还是Express?如果Express,'.'对连接字符串的服务器部分听起来不正确,并且您应该使用'。\ SQLEXPRESS'(或'(localdb)'或类似的名称) – Jcl