Spe*_*ets 6 c# sql-server asp.net-mvc entity-framework
访问数据库时出错.这通常意味着与数据库的连接失败.检查连接字符串是否正确,以及是否使用了相应的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; }
}
Run Code Online (Sandbox Code Playgroud)
这是员工上下文模型类
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是EmployeeControler
public class EmployeeController : Controller
{
public ActionResult Details()
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeID == 2);
return View(employee);
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
这是连接字符串
<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>
Run Code Online (Sandbox Code Playgroud)
实际视图没什么特别之处
@{
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>
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪
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()
Run Code Online (Sandbox Code Playgroud)
原因:当您使用IIS时,您的App Pool用户可能是"ApplicationPoolIdentity".连接到网络资源时,将作为本地系统连接(因此,如果在域中,则为domain\computer $帐户).据推测,该帐户无权访问SQL DB.
修复类型:您对IIS Express的修改"已修复"此连接是以当前用户身份进行的.如果您计划部署到IIS,那就不好了.
更好的修复:将您的IIS应用程序池用户更改为真正的Windows用户帐户,该帐户可以访问SQL DB.
我刚刚完成了同样的问题,并且可以确认上述修复程序对我有效.
经过对整个事情的相当多的挣扎之后,这对我有用:
-1) 卸载的 SQL Server
-2) 重新安装 SQL Server 并设置新实例(INST01)
^^我认为以上任何内容实际上都没有做任何事情。
-3) 通过转到 SQL Server 对象资源管理器并找到新实例,使用 Visual Studio 重新连接到新服务器。
-4) 返回 SQL Server Management Studio 并重新创建我的简单数据库。
-5) 在 SQL Server 对象资源管理器下,我从数据库属性复制了连接字符串。
-6) 我将项目的 Web 属性从本地 IIS 更改为 IIS Express。
-7) 卸载EntityFramework 6
-8) 删除了 web.config 文件中对实体框架的任何引用(这样 EF 5 的代码将在安装时插入)
-9) 从包管理器控制台安装了 EntityFramework 5.0.0。
-10) 运行应用程序并且它有效。
我不是 100% 确定问题是什么,但我认为它与本地 IIS 和 IIS Express 有关。我认为更改 EF 框架版本对于确保调用表的语法一致是必要的。
请记住,在这一切之前,我只需进入 SQL Server 对象资源管理器即可建立与数据库的连接。我什至可以在 Visual Studio 中调出表格并查看数据。
希望这对某人有帮助:)
| 归档时间: |
|
| 查看次数: |
15143 次 |
| 最近记录: |