2016-02-05 134 views
-1

我是一個新手,並試圖學習如何在C#中實現數據庫的代碼優先方法。我跟着this guide of Microsoft但我的代碼無法運行。 試圖修復它在互聯網上一整天,但無法找到任何解決方案,我的情況。我繼續前進,我非常需要你的幫助。SqlException是未處理的錯誤C#

你可以在教程中找到代碼,但爲了方便起見,你也可以在下面找到它。

它stucks在db.Blogs.Add(博客)線,它提供了以下錯誤:

SqlException was unhandled. Additional information Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
namespace CodeFirstNewDatabaseSample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (var db = new BloggingContext()) 
      { 
       // Create and save a new Blog 
       Console.Write("Enter a name for a new Blog: "); 
       var name = Console.ReadLine(); 

       var blog = new Blog { Name = name }; 
       db.Blogs.Add(blog); 
       db.SaveChanges(); 

       // Display all Blogs from the database 
       var query = from b in db.Blogs 
          orderby b.Name 
          select b; 

       Console.WriteLine("All blogs in the database:"); 
       foreach (var item in query) 
       { 
        Console.WriteLine(item.Name); 
       } 

       Console.WriteLine("Press any key to exit..."); 
       Console.ReadKey(); 
      } 
     } 
    } 

    public class Blog 
    { 
     public int BlogId { get; set; } 
     public string Name { get; set; } 

     public virtual List<Post> Posts { get; set; } 
    } 

    public class Post 
    { 
     public int PostId { get; set; } 
     public string Title { get; set; } 
     public string Content { get; set; } 

     public int BlogId { get; set; } 
     public virtual Blog Blog { get; set; } 
    } 

    public class BloggingContext : DbContext 
    { 
     public DbSet<Blog> Blogs { get; set; } 
     public DbSet<Post> Posts { get; set; } 
    } 
} 

下面是對現在的服務器資源管理器的情況:

enter image description here

下面是SQl服務器對象瀏覽器的情況。

enter image description here

以下是建議之後,我的app.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <connectionStrings> 
    <add name="BloggingContextDb" connectionString="Data Source=|DataDirectory|BloggingContext.sdf" providerName="System.Data.SqlServerCe.4.0"/> 
    </connectionStrings> 
</configuration> 
+2

什麼是完整的異常消息和'StackTrace'到底是什麼? –

+0

代碼有什麼問題?例如,你看到什麼編譯時或執行時錯誤? – RoadWarrior

+0

現在添加這些信息 –

回答

2

在這種類型的情況下,會的DbContext嘗試爲你創建數據庫。無論是在SQLExpress中(如果在本地安裝的話)(默認情況下安裝在VS2010中),或者在LocalDb(如果安裝在本地安裝時默認安裝了VS2012)。

您可以使用Visual Studio中的「服務器資源管理器」窗口來查找本地安裝的SQL Server實例。我懷疑沒有本地安裝,或者代碼由於某種原因無法訪問它(認證,授權等)。

這就是我在VS2013中看到的,當使用SQL Server服務器對象資源管理器窗口。它看起來像你的第二個屏幕截圖,你有安裝LocalDb。你能手動連接到它嗎?您可能需要登錄specify an explicit connection string that DbContext can use。這通常在app.config完成 - 請參閱創建app.config文件的鏈接。

enter image description here

+0

我應該在哪裏看到本地安裝?有服務器資源管理器中顯示的文件,我將分享我的答案。請參閱我添加的屏幕截圖。 –

+0

啊,你確實想讓我引用SQL Server Object Explorer。這也馬上就來了。 –

+0

已上傳。請再次參考。我們的瀏覽器的唯一區別就像我有2個數據庫。 –

2

我不知道你是怎麼結束與app.config中。
該配置無效EntityFramework使用(據我所知),並使用位於項目文件夾下的BIN \ DEBUG文件夾中的SDF文件。
順便說,日本自衛隊的文件是使用SQL Server精簡版文件,而不是由的SQL Server ExpressSql Server中的LocalDB

我都遵循相同的教程(只是使用Visual Studio 2015年代替),並在我的項目所產生的app.config是這個

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

唯一的問題(對我來說)是部分在那裏談論配置服務器資源管理器來查看數據庫文件。我必須使用(localdb)\mssqllocaldb作爲服務器名稱,而不是建議(localdb)\v11.0
(在App.config看參數鍵上方)

現在,在本教程的步驟4我運行程序,一切按預期工作。

所以我認爲你應該改變在app.config與上述內容或從頭開始重複本教程中,注意不要改變任何東西。如果錯誤一直存在,我只能建議切換到Visual Studio 2015年社區版(免費)

+0

我會在一小時內嘗試,當我嘗試我會通知你。謝謝你對我的興趣。 –

+0

瞧!我很高興你和RoadWarrior。雖然最後一擊擊中了你的目標,但我認爲RoadWarrior以這種方式引導了我們,所以我會支持你們,但我會接受他的回答。但我仍然很高興你!我可以問你,如果我必須重複每個項目的app.config過程?由於app.config在項目中,其他人將無法看到我猜的app.config。你能否解釋一下,我想知道你在那裏做了什麼。非常感謝! –

+0

正如我在第一條評論中所說的那樣,從錯誤消息中可以清楚地看到。奇怪的是app.config亂七八糟。我認爲,只有當你從一開始就重試教程時,你才能知道這個問題是否會重演自己 – Steve