【发布时间】:2019-11-08 05:03:18
【问题描述】:
我有模型文件夹,在文件夹中我有 Todo.cs 和 TodoContext.cs
在 Todo.cs 我的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ApiCrudWithEfCore.Models
{
public class Todo
{
public int Id { get; set; }
public string title { get; set; }
public bool Iscomplete { get; set; }
}
}
在 TodoContext 我有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace ApiCrudWithEfCore.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options) :base(options) {}
public DbSet<Todo> Todos { get; set; }
}
}
之后,我将连接字符串放入 appsettings.json :
{ 连接:“服务器=(localdb)\mssqllocaldb;数据库=Todo;Trusted_Connection=True;”, “记录”:{ “日志级别”:{ “默认”:“警告” } }, “允许的主机”:“*” }
我在 startup.cs 中这样使用它:
public void ConfigureServices(IServiceCollection services,IConfiguration config)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
}
但是当我使用 Add-Migration 命令时,它给了我这个错误:
访问“程序”类上的 IWebHost 时出错。 在没有应用程序服务提供商的情况下继续。错误: ConfigureServices 方法必须要么是无参数的,要么只接受一个 IServiceCollection 类型的参数。无法创建对象 输入“TodoContext”。对于设计支持的不同模式 时间,见https://go.microsoft.com/fwlink/?linkid=851728
我读过,但找不到对我有帮助的人可以帮助我吗?
【问题讨论】:
-
那是完整的 appsettings.json 吗?
-
@Haytam 是的
标签: c# asp.net .net asp.net-core entity-framework-core