【问题标题】:ASP.NET Entity Framework 7 config.json ConnectionStringASP.NET 实体框架 7 config.json ConnectionString
【发布时间】:2015-12-17 00:59:38
【问题描述】:

我在 MVC 6 ASP.NET 应用程序中创建数据库时遇到问题。当我尝试通过执行“dnx ef migrations add initial”在cmd中添加迁移时,我收到异常 “该值不允许为NULL。参数名称:connectionString”(它是从德语翻译过来的,所以英文直译可能会有所不同)

我搜索了整个网络,但找不到任何有用的东西。

这是我的 config.json:

  {
    "AppSettings": {
      "SiteTitle": "Chronicus"
    },
    "Data": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }

这是我的 Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Chronicus.models;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.AspNet.Identity.EntityFramework;


namespace Chronicus
{
    public class Startup
    {

        public IConfiguration Configuration { get; set; }

        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {

            // Setup configuration sources.
            var builder = new ConfigurationBuilder()
                .SetBasePath(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        // 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 http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ChronicusContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

            /*
            // auto generated code
            app.UseIISPlatformHandler();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });

            */
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc();
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

这是我的 project.json

    {
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta5",
    "Microsoft.Framework.Logging": "1.0.0-beta8",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "EntityFramework.Commands":  "7.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

有谁知道,如何解决这个问题?谢谢!

【问题讨论】:

    标签: c# asp.net json asp.net-mvc entity-framework


    【解决方案1】:

    除非您在第一个块中有错字,否则这似乎不是有效的 json,因为缺少一个开头 {

      {
        "AppSettings": {
          "SiteTitle": "Chronicus"
        },
        "Data": {
          "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
      }
    }
    

    应该是

      {
        "AppSettings": {
          "SiteTitle": "Chronicus"
        },
        "Data": {
          "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
      }
    

    注意两个开头{

    编辑

    看来您的 config.json 可能不正确。请使用以下示例格式化您的配置

    {
        "AppSettings": {
            "SiteTitle": "WebApplication2",
        },
        "Data": {
            "DefaultConnection": {
                "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-WebApplication1-414415dc-a108-49f3-a5e3-fdc4cf24ef96;Trusted_Connection=True;MultipleActiveResultSets=true"
            }
        }
    }
    

    【讨论】:

    • 这只是复制错误,所以它不会解决问题,但仍然感谢您查看我的问题。
    • 现在{} 太多了。删除第一个和最后一个。
    • @davidrue 你能验证每个.json 配置文件是否有效吗?如果需要,您可以在线轻松找到 JSON 验证器。
    • 好吧,好像它无效,我删除了第一个和最后一个,现在出现异常:“该值不允许为 NULL。参数名称:connectionString”(翻译自德语,所以在英语中直译可能会有所不同)
    • 在 config.json 中将 ConnectionString 更改为 connectionString
    【解决方案2】:

    它的错误的json,你在这里设置配置来获取属性:

    Configuration["Data:DefaultConnection:ConnectionString"])
    

    你的 json 需要是这样的:

    {   
    "AppSettings": {
          "SiteTitle": "Chronicus"   
    },   
    "Data": {
            "DefaultConnection": {
              "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Chronicus;Trusted_Connection=True;MultipleActiveResultSets=true"
                                 }   
            } 
    }
    

    其他问题可以采取配置的方法, 试试这个:

    Configuration.Get("Data:DefaultConnection:ConnectionString");
    

    而不是这个:

    Configuration["Data:DefaultConnection:ConnectionString"];
    

    欲了解更多信息,请尝试this link.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2015-08-05
      • 2016-04-16
      • 2010-10-07
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多