【发布时间】:2014-03-26 02:45:56
【问题描述】:
访问数据库时出错。这通常意味着与数据库的连接失败。检查连接字符串是否正确,以及是否使用了适当的 DbContext 构造函数来指定它或在应用程序的配置文件中找到它。
我发誓我什么都试过了!阅读本教程并努力获得与数据库的连接。
我在 SQL Server 2012 中的表名为“tblEmployee”,数据库名为“Sample”
这是 Employee.cs 类
[Table("tblEmployee")]
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Gender { get; set; }
}
这是员工上下文模型类
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
这是 EmployeeControler
public class EmployeeController : Controller
{
public ActionResult Details()
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeID == 2);
return View(employee);
}
}
这是 web.config 文件
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
这是连接字符串
<connectionStrings>
<add name="EmployeeContext" connectionString="Data Source=ADMIN-PC;Initial Catalog=Sample;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" />
</connectionStrings>
实际视图没有什么特别的
@{
ViewBag.Title = "Employee Details";
}
<h2>Employee Details</h2>
<table style="font-family:Arial">
<tr>
<td>EmployeeID:</td>
<td>@Model.EmployeeID</td>
</tr>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender:</td>
<td>@Model.Gender</td>
</tr>
<tr>
<td>City:</td>
<td>@Model.City</td>
</tr>
</table>
这是堆栈跟踪
at System.Data.Entity.ModelConfiguration.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Single[TSource](IQueryable`1 source, Expression`1 predicate)
at MVCDemo.Controllers.EmployeeController.Details() in c:\Users\Admin\Documents\Visual Studio 2013\Projects\MVCDemo\MVCDemo\Controllers\EmployeeController.cs:line 19
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f<InvokeActionMethodFilterAsynchronously>b__49()
【问题讨论】:
-
那么你的SQL Server叫ADMIN-PC,window用户(应用程序池)有权限吗?使用 SQL Sever Management Studio 验证您是否可以连接到数据库。
-
查看 InnerException,“提供程序没有返回 ProviderManifestToken 字符串。”啊!
-
是的@ElectricLlama,我打开了 Management Studio,我可以连接到数据库。这就是我最初用来制作数据库的方法。我什至通过在我的桌面上创建一个 Test.udl 文件并测试连接来测试连接,它工作正常! .udl 文件上的服务器名称列为“ADMIN-PC”。我认为这与我拥有的 EF 代码有关
-
接下来,您使用的是
Integrated Security=True,这意味着使用windows帐户进行连接。通过 SSMS 和 UDL,这就是你。通过您的应用程序,这可能是应用程序池标识。您可以尝试使用 SQL Profiler 来比较成功和失败的登录尝试,它会告诉您哪个 Windows 用户正在尝试登录(如果有)。您也可以暂时尝试使用 SQL 用户来验证这是否是问题所在。 -
这是我在检查 Manifest 错误时发现的:stackoverflow.com/questions/4741499/…
标签: c# sql-server asp.net-mvc entity-framework