是否可以将asp.net core 2.1 web api与sql server中的现有数据库连接起来?

时间:2018-06-15 10:01:42

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

我看到视觉工作室的官方视频展示了如何做到这一点。但最后,教师无法使其工作,他必须在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;
    }
}

My 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"
  }
}

Anh它不起作用。错误:

enter image description here

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

错误是由于您在ConnectionString中使用的appsetting.json而引起的,只需按照您的要求在appsetting.json1 and tweak the连接字符串`中尝试此代码即可。

ASP.NET Core Web API

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