简体   繁体   English

使用 ASP.NET Web API 向数据库添加数据时出现 SQL Server 错误

[英]Getting SQL Server error when adding data to database using ASP.NET Web API

I am trying to add some data to a ASP.NET Web API from the same solution, but somehow I am getting this error from SQL Server.我试图从同一解决方案向 ASP.NET Web API 添加一些数据,但不知何故我从 SQL Server 收到此错误。

This is my context这是我的上下文

public class SampleCtxt: DbContext
{
    public DbSet<TodoItem> TodoItems { get; set; }

    public SampleCtxt(DbContextOptions<SampleCtxt> options)
        : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=APITESTDB; Initial Catalog=APITestDb; Trusted_Connection=True;");
    }
}

Configure services method from API从 API 配置服务方法

public void ConfigureServices(IServiceCollection services)
{
        services.AddDbContext<SampleCtxt>(opt =>
            Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; Database = APITESTDB;"));
            opt.UseSqlServer(
                Configuration
                .GetConnectionString("DefaultConnection")));
        services.AddControllers();
}

Connection string from json来自 json 的连接字符串

"ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQLEXPRESS;Database=APITESTDB; Initial Catalog=APITestDb Trusted_Connection=True;"
},

Adding data from another console project从另一个控制台项目添加数据

static void Main(string[] args)
{
    using (SampleCtxt ctxt = new SampleCtxt(
        new Microsoft.EntityFrameworkCore.DbContextOptionsBuilder<SampleCtxt>().Options))
        {
            TodoItem todoItem = new TodoItem() { Name = "qualquer" };
            ctxt.TodoItems.Add(todoItem);
            ctxt.SaveChanges(); 
        }
}

Everything seems fine but I am getting this error:一切似乎都很好,但我收到此错误:

Verify that the instance name is correct and that SQL Server is configured to allow remote connections.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

Its seens that the ConnectionString was wrong and the instantiation of the class context, I solved the problem by adding a parameterless constructor and by correcting the OnConfiguring Method它看到 ConnectionString 是错误的,并且类上下文的实例化,我通过添加一个无参数构造函数并通过更正 OnConfiguring 方法解决了这个问题

public class SampleCtxt: DbContext
{
    public DbSet<TodoItem> TodoItems { get; set; }

    public SampleCtxt()
    {

    }

    public SampleCtxt(DbContextOptions<SampleCtxt> options)
        : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=APITESTDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用asp.net Web应用程序将数据插入SQL Server数据库,出现错误 - Trying to insert data into SQL Server database using asp.net web application, getting an error 从SQL Server Express迁移到SQL Server asp.net Web应用程序登录时出现错误 - Getting an error when moving from SQL Server Express to SQL Server asp.net web app logon 使用 AWS 中的 SQL Server 数据库发布 ASP.NET WEB API 2 - Publish ASP.NET WEB API 2 with SQL Server database in AWS ASP.NET Web API将图像上传到SQL Server数据库 - ASP.NET Web API upload image to SQL Server database 通过Asp.net Web API在SQL Server数据库中插入数据 - Insert data through Asp.net web API in sql server database 在ASP.NET Web窗体中使用ASP.NET Web API时出现“方法不允许”错误 - Getting “Method Not Allowed” error when using ASP.NET Web API inside ASP.NET Web Forms 无法使用asp.net Web应用程序将数据插入SQL Server数据库 - Cannot Insert data into SQL Server database using a asp.net web app Chart.js - 使用 sql server 和 asp.net mvc5 从数据库中获取数据 - Chart.js - Getting data from database using sql server and asp.net mvc5 当我在MVC中使用ASP.NET Web API时,无法在JSON响应中获取列表数据 - Not Getting the list data in json responce when i am using the asp.net web api in mvc 使用其他计算机时,A​​SP.net应用程序中出现SQL Server错误 - Getting SQL Server error in ASP.net application when using different computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM