0

我試圖設置一個項目與「洋蔥樣」結構,其中我使用多個層的不同部分該項目(DAL,BL,WEB)。起初,我將所有文件放在一個單獨的項目中,包括EF的依賴項,其中一切都像魅力一樣工作。EF 7 dnx數據庫更新命令導致初始化字符串的格式不符合從索引0開始的規範

然後我開始分割應用程序的不同部分,併爲每個「區域」創建較小的項目。

我的文件夾結構如下所示:

enter image description here

我決定從頂層(wwwroot文件),並安裝它的依賴到我的DAL項目打破了實體框架。 EF運行良好,我可以當我運行命令

dnx ef migrations add somemigrationname 

它增加了遷移文件夾到DAL項目,並與我期望的遷移填充它。 Howerver,當我嘗試更新使用

dnx ef database update 

它拋出下面的錯誤我的數據庫:

Format of the initialization string does not conform to specification starting at index 0. 

所有的答案,對於這個錯誤,我發現這裏的堆棧溢出廣大指向一個連接字符串無效。

奇怪的是我的連接字符串工作時,我使用ef和更新我的數據庫在頂層(我的wwwwroot項目)。但是當我開始使用不同的圖層後,它停止工作。

我的DAL層使用自己的啓動文件的DIT看起來是這樣的:

public class Startup 
{ 
    public IConfigurationRoot Configuration { get; set; } 

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) 
    { 
     var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 
     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 


    public void Configure(IApplicationBuilder app) 
    { 

     // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
    } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<CustomContext>(options => 
       options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 

     services.AddIdentity<Entities.ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<CustomContext>() 
      .AddDefaultTokenProviders(); 
    } 

編輯: 我DAL項目中我appsettings.json文件看起來像這樣:

{ 
    "Data": { 
    "DefaultConnection": { 
     "ConnectionString": "Data Source=.\\SQLEXPRESS;Initial Catalog=customdb;User ID=Carl; Password=test; Integrated Security=false;" 
    } 
    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Verbose", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    } 
} 

值得一提的是,我所有的項目都只針對dnxcore50 {},而不是dnx451。

+0

你只是測試你的連接字符串? –

+0

連接字符串與我在將EF移至其自己的項目之前所使用的那個連接字符串(工作過的)完全相同。 –

+0

「。\\ Sqlexpress」對我來說似乎很奇怪,因爲你在字符串前面加了@,我認爲它應該是「。\ Sqlexpress「 –

回答

0

你的錯誤是在你的連接字符串:

var connection = @"Data Source=.\\SQLEXPRESS;Initial Catalog=customdb;User ID=username; Password=mypassword; Integrated Security=false;"; 

它必須是:

var connection = @"Data Source=.\SQLEXPRESS;Initial Catalog=customdb;User ID=username; Password=mypassword; Integrated Security=false;"; 

var connection = "Data Source=.\\SQLEXPRESS;Initial Catalog=customdb;User ID=username; Password=mypassword; Integrated Security=false;"; 

,如果你不想用就用逐字串。

更多關於字符串和C#vermatim字符串,閱讀:string (C# Reference)

相關問題