C# .net核心sql server中的appsettings
我在尝试连接SQL Server中的数据库以在.net core中创建Web Api时遇到问题 这是我的代码: appsettings.jsonC# .net核心sql server中的appsettings,c#,sql-server,asp.net-core,C#,Sql Server,Asp.net Core,我在尝试连接SQL Server中的数据库以在.net core中创建Web Api时遇到问题 这是我的代码: appsettings.json { "ConnectionStrings": { " "DefaultConnection": "Server=ipserver,portserver;Database=Equipamiento;user id=userserver;password=password;Trusted_Connection=True;MultipleAct
{
"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; }
}
}
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;
}
}
}
数据/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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
命名空间verandrangularcore.Data
{
公共类TipoEquipoContext:DbContext
{
公共TipoEquipoContext(DbContextOptions选项):基本(选项){}
公共TipoEquipoContext(){}
公共DbSet-TipoEquipoMaster{get;set;}
}
}
Data/TipoEquipoContext.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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
命名空间verandrangularcore.Data
{
公共类TipoEquipoContext:DbContext
{
公共TipoEquipoContext(DbContextOptions选项):基本(选项){}
公共TipoEquipoContext(){}
公共DbSet-TipoEquipoMaster{get;set;}
}
}
控制器/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; }
}
}
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用VeranderAngularCore.数据;
//有关为空项目启用Web API的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=397860
命名空间VeranderAngularCore.Controllers
{
[产生(“应用程序/json”)]
[路线(“api/TipoEquipoMasterAPI”)]
公共类TipoEquipoMasterAPI:控制器
{
私有只读TipoEquipoContext\u上下文;
公共TipoEquipoMasterAPI(TipoEquipoContext上下文)
{
_上下文=上下文;
}
//获取:api/值
[HttpGet]
[路线(“蒂波基波”)]
公共IEnumerable GetStudentMasters()
{
return_context.TipoEquipoMaster;
}
}
}
在尝试进入时:
我收到以下错误:加载资源失败:net::ERR\u CONNECTION\u RESET您好,是否为DbContext添加了服务 在Startup.cs ConfigureServices方法中,尝试添加
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TipoEquipoContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
}
我不确定你是否已经有了这个。。。嗯 如果您有任何多对多关系,那么对象图中的循环可能会有问题,因为此时EFCore不支持延迟加载 如果是这样,您可以将序列化程序配置为不遵循图形循环 在Startup.cs文件中,将
services.AddMvc()
行替换为以下内容:
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
您的连接字符串中有一个额外的“”。请尝试更正此错误,但是ERR_connection_RESET可能会提示网络问题复制错误的代码,我有它:“DefaultConnection”:“Server=ipserver,portserver;数据库=设备;用户id=用户服务器;密码=密码;Trusted_Connection=True;MultipleActiveResultSets=true;“可以发布startup.cs代码吗?