ASP.NET Core和Entityframwork迁移错误

时间:2019-06-26 09:36:10

标签: c# asp.net .net asp.net-core entity-framework-core

我有模型文件夹,并且在文件夹中有 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;”,     “记录”:{       “ LogLevel”:{         “默认”:“警告”       }     },     “允许的主机”:“ *”   }

我像这样在startup.cs中使用它:

 public void ConfigureServices(IServiceCollection services,IConfiguration config)
        {

            services.AddMvc();

            services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
        }

但是当我使用Add-Migration命令时,它给了我这个错误:

  

在类“ Program”上访问IWebHost时发生错误。   没有应用程序服务提供商就继续进行。错误:   ConfigureServices方法必须是无参数的或仅采用一个   IServiceCollection类型的参数。无法创建的对象   输入“ TodoContext”。针对设计支持的不同模式   时间,请参见https://go.microsoft.com/fwlink/?linkid=851728

我读过,但找不到对我有帮助的人,有人可以帮助我吗?

4 个答案:

答案 0 :(得分:2)

您也可以尝试通过这种方式注册数据库上下文:

public IConfiguration Configuration { get; }

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

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(options => options.UseSqlServer(Configuration.GetConnectionString("connection")));
    ...
}

或者您可以手动注册数据库上下文:

services.AddTransient<TodoContext>();

答案 1 :(得分:1)

使用GetConnectionString时,您的appsettings.json应该包含一个名为 ConnectionStrings 的部分,您需要在其中将连接名称(例如“ todo” ”),并设置实际的连接字符串。

示例:

"ConnectionStrings": {
    "todo": "Server=(localdb)\mssqllocaldb;Database=Todo;Trusted_Connection=True;"
},
-- Rest of your appsettings

然后,您将像这样使用它:config.GetConnectionString("todo")

this引用,GetConnectionString是:

  

GetSection(“ ConnectionStrings”)[name]的简写。

答案 2 :(得分:1)

请将您的配置更改为:

"ConnectionStrings": {
    "connection": "Server=Yourserver;Database=DAWIDARI;Trusted_Connection=true;"
},

并在Startup.cs中调用它

 services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));

答案 3 :(得分:-1)

我首先发现我的问题,然后按照建议更改了appsettings.json文件,它看起来像这样:

{
  "ConnectionStrings": { "todo": "Server=(localdb)\\mssqllocaldb;Database=Todo;Trusted_Connection=True;" },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

但是它并不能解决问题,因此我开始检查startup.cs并更改了注入应用程序设置的方式,因此我的startup.cs文件如下所示:

        public IConfiguration _config { get; set; }
    public Startup(IConfiguration configuration) {
        _config = configuration;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();

        services.AddDbContext<TodoContext>(options => options.UseSqlServer(_config.GetSection("ConnectionStrings")["todo"]));
    }

我在启动构造函数中注入Iconfiguration并定义_config var并为其分配配置,之后我使用 GetSection 获取连接字符串

相关问题