【问题标题】:Is it possible to connect asp.net core 2.1 web api with existing database in sql server?是否可以将 asp.net core 2.1 web api 与 sql server 中的现有数据库连接?
【发布时间】:2018-11-25 03:57:38
【问题描述】:

我看到 Visual Studio 的官方视频展示了如何做到这一点。但最后导师还是不行,他必须在 Startup.cs 中用 UseInMemoryDatabase() 修改 UseSqlServer()。

这是我的代码,我按照他的指示进行操作。 首先,创建一个文件,其中一个类包含 Dbcontext,其他类映射到数据库中的表。就我而言,数据库只有一张表:学生。这是包含 Dbcontext 的类:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HoangHoaTham.WebAPI.Entities
{
    public class TodoContext : DbContext
    {
        public TodoContext(DbContextOptions<TodoContext> options) : base(options)
        {
        }

        public DbSet<Students> Students;
    }
}

我的 Startup.cs,我在这里添加 UseSqlServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HoangHoaTham.WebAPI.Entities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace HoangHoaTham
{
    public class Startup
    {   
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TodoContext>(options
                => options.UseSqlServer(Configuration.GetConnectionString("HoangHoaTham")));



            services.AddMvc();
        }

        //You don't need to reed the code below

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}

最后,我将 ConnectionString 添加到 appsetting.json 中:

{
  "ConnectionString": {
    "HoangHoaTham": "Server=PC-PC\\SQLEXPRESS;Database=HoangHoaThamHighSchool;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

嗯,它不起作用。错误:

enter image description here

这是他的视频:https://www.youtube.com/watch?v=aIkpVzqLuhA 跳到 50:47 看看他是怎么做到的。 非常感谢。

【问题讨论】:

  • 那么您的问题是什么?
  • 我想知道 .net core web 是不是真的无法连接到 sql server,还是我错过了成功的东西
  • 答案是肯定的。如果您有连接异常,您应该在问题中发布错误消息。
  • 您是否尝试过您的代码。行得通吗。他视频中的问题是一些许可问题。也许你的电脑上没有这个。
  • 你的电脑真的叫PC-PC吗??你有 SQL express 或 SQL developer 吗?

标签: c# database-connection asp.net-core-webapi


【解决方案1】:

错误在于您在appsetting.json 中使用的ConnectionString,只需根据您的要求在appsetting.json1 and tweak the connectionstring` 中尝试此代码即可。

ASP.NET Core Web API

{
   "ApplicationInsights":{
      "InstrumentationKey":""
   },
   "ConnectionStrings":{
      "DefaultConnection":"Server=srvrName;Database=Test;User Id=User;Password=pass"
   },
   "Logging":{
      "LogLevel":{
         "Default":"Warning"
      }
   },
   "AllowedHosts":"*"
}

【讨论】:

    【解决方案2】:

    错误告诉你Configuration.GetConnectionString("HoangHoaTham") 的结果是null。因此,您的配置存在一些问题,结果是:appsettings.json 中的连接字符串部分应该是ConnectionStrings(复数)。你有ConnectionString(单数)代替。

    【讨论】:

    • 这是我的错误的一部分。我也忘了在 DbsetStudents 添加 {get;set;} ,所以它不起作用。现在可以了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2019-04-12
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    相关资源
    最近更新 更多