1.修改构造函数
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace WEBAPI.EF
{
public class LabelContext:DbContext
{
public DbSet<LabelInfo> LabelInfo { get; set; }public LabelContext(DbContextOptions<LabelContext> options) : base(options)
{}
}
}
2. 在startup.cs中加入使用连接字符串
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<LabelContext>(options =>
{
var connectionString = this.Configuration["ConnectionStrings:Default"];
options.UseSqlServer(connectionString);
});services.AddControllers();
3.在appsettings.json中设置数据库字符串
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"urls": "http://*:5000",
"ConnectionStrings": {
"Default": "Server=192.168.1.112; Database=LabelingSystem; Trusted_Connection=False; uid=user; pwd=user; MultipleActiveResultSets=true;"
}
}