2013-02-12 88 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管道。

干杯。