2015-12-17 25 views
0

我在創建MVC 6 ASP.NET應用程序中的數據庫時遇到了問題。當我嘗試通過執行「dnx ef migrations add initial」來添加cmd中的遷移時,我收到異常 「該值不允許爲NULL。Parametername:connectionString」(它是從德語翻譯的,所以在英語中直接翻譯可能會不同)ASP.NET Entity Framework 7 config.json 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" 
    ] 
} 

有誰知道,如何解決這個問題?謝謝!

回答

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" 
     } 
    } 
} 
+0

這是不對的複製,所以它不會解決問題,但還是謝謝你看我的問題。 – davidrue

+1

現在你有太多'{'和'}'。刪除第一個和最後一個。 – DavidG

+0

@davidrue你可以驗證每個'.json'配置文件是否有效嗎?如果你需要,你可以很容易地在網上找到JSON驗證器。 – Seany84

0

錯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.