代码之家  ›  专栏  ›  技术社区  ›  Caverman

实体框架核心添加迁移提供的值不能为空(参数'connectionString')

  •  0
  • Caverman  · 技术社区  · 6 年前

    值不能为空(参数'connectionString')

    ConnectionStrings 在 appsetttings.json 文件。不过,我已经找遍了我的,并尝试了几个复制/粘贴,以确保我有正确的拼写。要么是别的原因,要么是我看了太多,忽略了什么。

    Appsettings.json文件

    {
        "ConnectionStings": {
            "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
        },
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },
        "AllowedHosts": "*"
    }
    

    启动.cs

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; }
    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DbConnection")));
        services.AddControllers();
        services.AddAutoMapper(typeof(Startup));
        services.AddScoped<ICharacterService, CharacterService>();
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   olk    6 年前

    在你的例子中你拼错了 连接字符串

     {
            "ConnectionStings": {
                "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
            },
            "Logging": {
                "LogLevel": {
                    "Default": "Information",
                    "Microsoft": "Warning",
                    "Microsoft.Hosting.Lifetime": "Information"
                }
            },
            "AllowedHosts": "*"
        }
    
        2
  •  0
  •   Karthik    6 年前

    应用程序设置和配置服务方法看起来不错。 试试这个

      "ConnectionStrings": {
        "DbContext": "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=dotnet-rpg;Integrated Security=SSPI; MultipleActiveResultSets=true;",
        "Logging": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        }
      }
    

    services.AddDbContextPool<DataContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DbContext"))
                );
    

    如果这解决了你的问题,请告诉我。

    下面两个参考链接有实现细节。

    Implement create, read, update, and delete functionalities using Entity Framework Core

    Configure Azure SQL relational database using vs 2019

    推荐文章