2016-10-26 125 views
2

对于实体框架,我非常新,所以如果这个问题是基本的,我很抱歉。App.Config中的实体框架连接字符串

我正在通过Code First教科书工作,并创建了一个小型类库(c#)和一个控制台应用程序。该教科书指出,当我运行它时,实体框架将自动在SQL服务器中构建数据库。它没有,我相信的原因是因为我有一个命名实例而不是默认实例\ SQLExpress。该教科书指出,在使用默认实例时,我不需要连接字符串,但由于我没有使用默认实例,所以我猜测我需要在App.Config中使用connectionm字符串。我曾尝试在此文件中放入标准连接字符串,但它不起作用。

这里是我的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> 

可能有人请告知在连接字符串去,或者告诉我,如果我要对这个错误的方式。

UPDATE

感谢帮助迄今收到,我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> 
    <connectionStrings> 
    <add name="Model" connectionString="Server=OFFICE-ACER\SQLE;Database=BreakAway;Trusted_Connection=True;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
    <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> 

然而,什么也没有发生,即没有创建数据库之前。

+0

您需要一个连接字符串。此外,配置中连接字符串的名称是您在创建实例时传递给DBContext的内容。 – Darren

回答

3

在您<configSections>标签,你可以把这个

<connectionStrings> 
    <add name="myConn" connectionString="theConnString" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

然后用你所需要的

而且虽然它可能无法帮助你在这种情况下替换值,以避免做手工在将来,EntityFramework NuGet包可以创造奇迹,您只需输入数据库的url和登录名 - 然后在您的配置文件中为您创建整个连接字符串,创建您的edmx并允许您导入您选择的数据

+0

这工作,但只有在我将“System.Data.EntityClient”更改为“System.Data.SqlClient”后。在此之前,我收到一个错误“关键字不支持'服务器'”。感谢您的帮助 - 它让我到了我需要的地方。 – PJW

+0

这是相当好的,抱歉没有抓住那个 –

1

这应做到:

<?xml version="1.0"?> 
<configuration> 
    <connectionStrings> 
     <add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/> 
    </connectionStrings> 
</configuration> 
+0

这听起来真的很愚蠢;但实体名称是什么。这是我的上下文的名称,数据库的名称,或类库项目(或其他)的名称? – PJW

+1

@PJW通常它和你的'DbContext'类的名字相同 – rbr94

1
<configuration> 
    <configSections> 
     ... 
    </configSections> 
    <connectionStrings> 
    <add name="Name" connectionString="Data Source=servername; Initial Catalog = yourDbName ; Intgrated Security ="According to your requirement" providerName="System.Data.SqlClient" /> 

    </connectionStrings> 

...

0

,你需要知道的第一件事。如果您正在使用类库(.dll),在.dll中创建的实体(.edmx)文件,并且您正在从MVC应用程序(具有web.config)调用此方法。 App.config中的连接字符串永远不会被使用。

因此,您可以将连接字符串映射到Web.Config(MVC项目)上,甚至可以从App.Config中删除。它将始终使用web.config。

+0

没有web.config文件 – PJW

+0

我没有看到任何提示这是一个MVC应用程序。通常,您需要为使用此模型的EDM定义连接字符串。这意味着,如果您在应用程序中使用包含EDM的DLL,则连接字符串需要放在应用程序的app-config中,而不是放在DLL的app-config中 – rbr94

相关问题