2013-02-12 73 views
0

我在關注本教程:http://msdn.microsoft.com/en-us/data/jj193542Code First基本使用ADO.NET EF。問題是,執行代碼時DataBase不是自動創建的,我認爲這是因爲我的app.config中的配置,但這是我第一天使用真實代碼,我無法弄清楚如何調整我的配置文件,所以我可以連接到我的服務器並根據需要使用創建的數據庫,或者讓本教程中的程序按照預期創建新數據庫。ADO.NET EF - 無法使用VS2010建立數據庫連接

這是我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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <entityFramework> 
    <!-- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v11.0" /> 
     </parameters> 
    </defaultConnectionFactory> --> 


    <connectionStrings> 
     <add name="BloggingContext" 
      providerName="System.Data.SqlClient" 
      connectionString="server=milka-pc\sqlserver2012;Database=Panorama;user id=MyID;password=MyPass;port=3333;Integrated Security=True;"/> 
    </connectionStrings> 

    <!--<contexts> 
     <context type=" Blogging.BloggingContext, MyAssembly"> 
     <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" /> 
     </context> 
    </contexts>--> 


    </entityFramework> 
</configuration> 

正如你可以看到,我嘗試了各種各樣的事情,使工作某些部分評論。

這是服務器資源管理器如何Ÿ樣子,而我試圖連接:

enter image description here

而且因爲我有零經驗ViasualStudioXXXX和連接到數據庫,這是我所看到的和我如何選擇我的服務器:

enter image description here

有在milka-pc\sqlserver2012我有一個名爲DB Panorama,我可以使用或更好的按照教程一步一步一步離開程序來創建我的新數據庫。現在我不能這兩個。並連接到milka-pc\sqlserver2012我需要提供UserNamePassword,我在App.config中做過。我這樣說只是爲了知道這些字段不爲空

回答

1

如果您確信米爾卡-PC \ sqlserver2012正在端口3333,其實如你所說

改變你的ConnectionString這樣:

<add name="BloggingContext" 
     providerName="System.Data.SqlClient" 
     connectionString="server=milka-pc\sqlserver2012,3333;Database=Panorama;user id=MyID;password=MyPass;Integrated Security=True;"/> 

只是爲了確保在您的SQL管理工作室中使用提供的憑據來嘗試連接字符串。

如果這不起作用,請諮詢您的SQL Server配置管理器,特別是在SQL server網絡配置下,確保您的實例已啓用啓用了適當端口的TCP/IP管道。

乾杯。