访问数据库时出错。这通常意味着与数据库的连接失败。检查连接字符串是否正确,以及是否正在使用相应的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()
答案 0 :(得分:8)
原因:当您使用IIS时,您的App Pool用户可能是&#39; ApplicationPoolIdentity&#39;。连接到网络资源时,将作为本地系统连接(因此,如果在域中,则为domain \ computer $帐户)。据推测,该帐户无权访问SQL DB。
修复方式:您对IIS Express的修改&#39;已修复&#39;这个,连接是作为当前用户。如果您计划部署到IIS,那就不好了。
更好的修复:将您的IIS应用程序池用户更改为真正的Windows用户帐户,该帐户可以访问SQL DB。
我刚刚完成了同样的问题,并且可以确认上述修复程序对我有效。
答案 1 :(得分:7)
我的主项目包含web.config文件(包含连接字符串)未设置为默认项目。将其设置为默认项目解决了问题。
答案 2 :(得分:1)
Soooo在经历了这一切的挣扎之后,这对我有用:
-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文件中对Entity Framework的任何引用(这样在安装时就会插入EF 5的代码)
-9)从软件包管理器控制台安装了EntityFramework 5.0.0。
-10)运行应用程序并且运行良好。
我不是100%确定问题是什么,但我认为它与本地IIS和IIS Express有关。我认为更改EF框架版本是必要的,以确保调用表的语法是一致的。
请记住,在所有这些之前,我可以通过进入SQL Server对象资源管理器建立与数据库的连接。我甚至可以在Visual Studio中拉出表格并查看数据。
希望这有助于某人:)
答案 3 :(得分:1)
我有类似的错误并已解决。当我设置“启动项目”时,我有两个以上的模块,该模块包含解决方案中的 ApplicationDbContext 类。右键单击模块>>单击“设置为启动项目”
答案 4 :(得分:0)
如果您的服务器是2012 sql express免费版,那么您的连接字符串应该是这样的。
<connectionStrings>
<add name="EmployeeContext" connectionString="Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Connect timeout=15;Encrypt=False;TrustServerCertificate=False"
providerName="System.Data.SqlClient" />
数据源应该是= Admin-PC \ SQLEXPRESS ...
答案 5 :(得分:0)
从这个类派生的类可以放在同一个程序集中 从 DbContext 派生的类,用于定义实体框架 应用程序的配置。通过调用设置配置 受保护的方法和设置此类的受保护属性 派生类型的构造函数。使用的类型也可以 在应用程序的配置文件中注册。
DbConfiguration派生类应该与DbContext在同一个程序集中。
在我的ADO.NET实体数据模型中,我发现我的类派生自DbContext,位于 Model1.edmx - &gt; Model1.Context.cs - &gt; Model1.Context.cs 即可。
代码假设配置您的清单令牌并希望它能解决您的问题。
我发现了类似的问题讨论here,希望可以帮助您解决问题。
好消息是要解决这个问题,你实际上不必更改EF代码。
EF6允许配置服务,其中一个服务允许覆盖获取提供者清单令牌的默认行为。
您可以使用基于代码的配置注册自定义清单令牌解析器,并返回硬编码值(例如,如果您始终针对SqlServer 2012运行,则只返回“2012”)或者提出你自己的版本。
这可以防止EF检查您的应用运行的数据库版本。
如果针对不同版本的Sql Server运行,则可以使用DbConnection.ServerVersion属性为数据库创建提供程序清单标记。
这是一个示例 - 您可以只复制/粘贴它应该可以工作,因为EF应该自动检测配置 * *(只要DbConfiguration派生类是与上下文)
在同一个程序集中public class Configuration : DbConfiguration
{
public Configuration()
{
SetManifestTokenResolver(new ManifestTokenResolver());
}
}
public class ManifestTokenResolver : IManifestTokenResolver
{
public string ResolveManifestToken(DbConnection connection)
{
// The simplest thing is just to return hardcoded value
// return "2012";
try
{
connection.Open();
var majorVersion =
Int32.Parse(connection.ServerVersion.Substring(0, 2), CultureInfo.InvariantCulture);
if (majorVersion == 9)
{
return "2005";
}
if (majorVersion == 10)
{
return "2008";
}
return "2012";
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
}
答案 6 :(得分:0)
在通过IIS托管我的MVC应用程序时,我也遇到了同样的问题。 错误:无法打开数据库&#34; MVCDemo&#34;登录请求。登录失败。用户&#39; IIS APPPOOL \ ASP.NET v4.0&#39;。
登录失败本地用户不进行部署的临时修复:我们可以使用IISExpress,Go to Project - &gt; Properties - &gt; Web - &gt;启用无线电,而不是在IIS上进行部署按钮,使用本地IIS服务器并选中设置使用IIS Express。
系统规格:VS 2012,