在.net核心SQL服务器中设置应用程序

问题描述:

当尝试连接到SQL Server中的数据库以在.net核心中创建Web Api时,出现问题。在.net核心SQL服务器中设置应用程序

这是我的代码:

appsettings.json

{ 
    "ConnectionStrings": { 
    " "DefaultConnection": "Server=ipserver,portserver;Database=Equipamiento;user id=userserver;password=password;Trusted_Connection=True;MultipleActiveResultSets=true;" 


    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    } 
} 

控制器/ TipoEquipoMasterAPI.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.ComponentModel.DataAnnotations; 
namespace VeranderAngularCore.Data 
{ 
    public class TipoEquipoMaster 
    { 
     [Key] 
     public int IDTipoEquipo { get; set; } 

     [Required] 
     [Display(Name ="TipoEquipo")] 
     public string TipoEquipo { get; set; } 

     [Required] 
     [Display(Name = "Estado")] 
     public int _Status { get; set; } 

    } 
} 

数据/ TipoEquipoMaster.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.EntityFrameworkCore; 
namespace VeranderAngularCore.Data 
{ 
    public class TipoEquipoContext:DbContext 
    { 
     public TipoEquipoContext(DbContextOptions<TipoEquipoContext> options) : base(options) { } 

     public TipoEquipoContext() { } 

     public DbSet<TipoEquipoMaster> TipoEquipoMaster { get; set; } 
    } 
} 

数据/ TipoEquipo Context.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.EntityFrameworkCore; 
namespace VeranderAngularCore.Data 
{ 
    public class TipoEquipoContext:DbContext 
    { 
     public TipoEquipoContext(DbContextOptions<TipoEquipoContext> options) : base(options) { } 

     public TipoEquipoContext() { } 

     public DbSet<TipoEquipoMaster> TipoEquipoMaster { get; set; } 
    } 
} 

控制器/ TipoEquipoMasterAPI.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Mvc; 
using VeranderAngularCore.Data; 

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 

namespace VeranderAngularCore.Controllers 
{ 
    [Produces("application/json")] 
    [Route("api/TipoEquipoMasterAPI")] 
    public class TipoEquipoMasterAPI : Controller 
    { 


     private readonly TipoEquipoContext _context; 

     public TipoEquipoMasterAPI(TipoEquipoContext context) 
     { 
      _context = context; 
     } 

     // GET: api/values 

     [HttpGet] 
     [Route("TipoEquipo")] 
     public IEnumerable<TipoEquipoMaster> GetStudentMasters() 
     { 
      return _context.TipoEquipoMaster; 

     } 

    } 
} 

,并试图进入时:http://localhost:14621/api/TipoEquipoMasterAPI/TipoEquipo 我收到以下错误:无法加载资源:净:: ERR_CONNECTION_RESET

+2

在连接字符串的Theres一个额外的“”“尝试正确的,但是在ERR_CONNECTION_RESET可能暗示 – Webbanditten

+0

复制的网络问题。代码错误,我有:“DefaultConnection”:“服务器= ipserver,端口服务器;数据库= Equipamiento;用户id = userserver;密码=密码; Trusted_Connection = True; MultipleActiveResultSets =真;” –

+0

可以发布startup.cs代码? – Anuraj

喜你为DbContext添加了服务吗?

在Startup.cs ConfigureServices方法

尝试添加

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddDbContext<TipoEquipoContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 
    } 

我不知道如果你已经有了这个... ... hmmmmm

如果您有任何许多一对多关系,因为EFCore目前不支持延迟加载,所以对象图中的循环可能会有问题。

如果是这样,你可以配置你的序列化程序不遵循图形循环。

在你Startup.cs文件,与此更换services.AddMvc()行:

services.AddMvc() 
    .AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);