【问题标题】:Asp.net core and Entityframwork migration errorAsp.net 核心和实体框架迁移错误
【发布时间】:2019-11-08 05:03:18
【问题描述】:

我有模型文件夹,在文件夹中我有 Todo.csTodoContext.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


【解决方案1】:

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

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>();

【讨论】:

    【解决方案2】:

    使用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] 的简写。

    【讨论】:

    • 我改变了它,但我认为我的主要问题在于 config 我认为我以错误的方式注入它
    【解决方案3】:

    请将您的配置更改为:

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

    并在 Startup.cs 中调用它

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

    【讨论】:

    • 我改变了它,但我认为我的主要问题在于 config 我认为我以错误的方式注入它
    【解决方案4】:

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

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

    但它并没有解决问题,所以我开始检查 startup.cs 并更改注入 appsettings 的方式,因此我的 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 获取连接字符串

    【讨论】:

    • 你试过我获取连接字符串的方法了吗?
    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多