简体   繁体   中英

ASP.NET WebAPI error: an exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

I get this error whenever I call any of the functions I've written in the controller.

The Controller (Web API 2 controller empty):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Test.DBA;
using Test.Models;

namespace Test.Controllers
{
    public class UserAPIController : ApiController
    {
        ApiDbContext dbContext = null;

        public UserAPIController()
        {
            dbContext = new ApiDbContext();
        }

        [HttpPost]
        public IHttpActionResult InsertUser(User user)
        {
            dbContext.Users.Add(user);
            dbContext.SaveChangesAsync();

            return Ok(user.Id);
        }

        public IEnumerable<User> GetAllUser()
        {
            var list = dbContext.Users.ToList();
            return list;
        }

        [HttpPost]
        public IHttpActionResult DeleteUser(User user)
        {
            dbContext.Users.Remove(user);
            dbContext.SaveChangesAsync();

            return Ok(user.Id);
        }

        [HttpGet]
        public IHttpActionResult ViewUser(int id)
        {
            var student = dbContext.Users.Find(id);
            return Ok(student);
        }

        [HttpGet]
        public IHttpActionResult UpdateUser(User user)
        {
            var std = dbContext.Users.Find(user.Id);

            std.Name = user.Name;
            //std.startTime = user.startTime;
            //std.endTime = user.endTime;
            //std.Gender = user.Gender;
            //std.Adress = user.Adress;

            dbContext.Entry(std).State = System.Data.Entity.EntityState.Modified;
            dbContext.SaveChangesAsync();

            return Ok();
        }
    }
}

The model classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        //public DateTime startTime { get; set; }
        //public DateTime endTime { get; set; }
        //public string Gender { get; set; }
        //public string Adress { get; set; }
    }
}

EDIT: connection string code added

<connectionStrings>
    <add name ="Connection" 
         connectionString="Data Source=\.SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>*

EDIT 2: ApiDbContext code added.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Test.Models;

namespace Test.DBA
{
    public class ApiDbContext :DbContext
    {
        public ApiDbContext() : base("Connection")
        {

        }

        public DbSet<User> Users { get; set; }
    }
}

It seems you are not able to connect to SQL Server. Have you checked your connection string in this manner?

Your wrote your connection string wrong!! Your wrote Data source = /.SQLEXPRESS , Actually it would be Data source = ./SQLEXPRESS and the whole connection string should be as follows:

    <connectionStrings>
       <add name ="Connection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
   </connectionStrings>

There is a small type, the hostname ( . this case) should be before the \\ seperator, change:

<connectionStrings>
   <add name ="Connection" connectionString="Data Source=\.SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

to:

<connectionStrings>
   <add name ="Connection" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=PonesiLoptu;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM