查看记录时出错

时间:2017-03-23 01:40:33

标签: asp.net iis asp.net-web-api visual-studio-2015

我遵循了本教程:http://csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-and-sql-server.html。我已按照以下方式将其部署到iis:https://www.youtube.com/watch?v=HwwvNft38Us并且最初使用网址:http://localhost/WebApplication10。但是,在尝试访问http://localhost/WebApplication10/api/users时,不会提供用户列表,而是会出现以下错误:

EntityFramework.SqlServer.dll中发生了'System.Data.Entity.Core.EntityException'类型的异常但未在用户代码中处理 附加信息:基础提供程序在Open上失败。

这是我弹出错误的UserController的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using UserDataAccess;

namespace WebApplication10.Controllers
{
public class UsersController : ApiController
{
public IEnumerable<User> Get()
{
    using (UsersDBEntities entities = new UsersDBEntities())
    {
        return entities.Users.ToList();
    }
}

public User Get(string name)
{
    using (UsersDBEntities entities = new UsersDBEntities())
    {
        return entities.Users.FirstOrDefault(u => u.Username == name);
    }
}
}
}

这是web.config文件的代码:

<connectionStrings>
<add name="UsersDBEntities" connectionString="metadata=res://*/UserDataModel.csdl|res://*/UserDataModel.ssdl|res://*/UserDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=BPS-DEV-M001;initial catalog=UsersDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

像这样打开数据库?

using (DatabaseEntities DBEntities = new DatabaseEntities())
{
    DBEntities.Connection.Open();
    // Follow your code here.....
}