实体模型无法在app config中找到连接字符串。

时间:2015-07-29 18:01:49

标签: c# vb.net entity-framework

我有一个实体模型包含在一个DLL中,我正在为它编写一些单元测试。当我运行它时,它说在应用程序配置中找不到连接字符串。

数据:  模型:

    <?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog" />
          <!-- Uncomment the below section to write to the Application Event Log -->
          <!--<add name="EventLog"/>-->
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" />
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
  <connectionStrings>
    <add name="efContext" 
         connectionString="metadata=res://*/Models.efModel.csdl|res://*/Models.efModel.ssdl|res://*/Models.efModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Production;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

连接字符串显然在那里。 当我的测试调用dll中的一个函数时,就像这样编写

Public Function Find(ByVal SectionId As Integer) As Data.Section Implements ISection.Find

            Dim SectId As String = SectionId.ToString()

            Using dataContext As efContext = New efContext()

                dataContext.Database.Connection.ConnectionString = _efConnectionString

                Dim c As Data.Section = Nothing

                c = (From cs In dataContext.Sections
                            Where cs.SectionId = SectId).FirstOrDefault()

                Return c

            End Using

        End Function

它在dataContext.Database.Connection.ConnectionString = _efConnectionString

上失败

虽然当我调试它时它已经有例外。

我尝试在单元测试项目中复制app config。我尝试将应用配置链接为快捷方式。我已经被困在这一整天了。我已经尝试了几乎我在堆栈溢出时看到的每个解决方案。它似乎没有用。

我甚至尝试重塑实体模型,但也没有帮助。 有什么想法吗?

0 个答案:

没有答案