实体框架4.0,找不到连接名称

时间:2010-10-26 17:31:37

标签: .net entity-framework entity-framework-4 connection-string

我正在尝试在项目中使用N-unit进行单元测试。该项目中包含.edmx文件,因此我看到很多人并不是一个多项目问题。我保留了工具自动生成的所有默认值。我的Web.config文件似乎在<connectionStrings>

中有连接字符串
<connectionStrings>
<add name="LCFEntities" connectionString="metadata=res://*/LCF.csdl|res://*/LCF.ssdl|res://*/LCF.msl;provider=System.Data.SqlClient;provider connection string='Data Source=xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=LCF_admin;Password=xxxx;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient" />
</connectionStrings>

在LCF.designer.vb中,它看起来正在寻找正确的名称:

 ''' <summary>
''' Initializes a new LCFEntities object using the connection string found in the 'LCFEntities' section of the application configuration file.
''' </summary>
Public Sub New()
    MyBase.New("name=LCFEntities", "LCFEntities")
MyBase.ContextOptions.LazyLoadingEnabled = true
    OnContextCreated()
End Sub

这一行错误Dim db As New LCFEntities()。该项目是一个Web应用程序项目,我为该解决方案创建了一个目录。不确定这是否重要。

错误是:

The specified named connection is either not found in the configuration, not intended         
to be used with the EntityClient provider, or not valid.

任何人都有任何线索?

修改

这是在Dim db as New LCFEntities()行上失败的测试:

<Test()> _
Public Sub insertTest()
    Dim areaName As String = "Test Category"

    Dim db As New LCFEntities()
    Dim area = db.CreateObject(Of Area)()
    area.DateCreated = Date.Now()
    area.DateModified = Date.Now()
    area.Id = Guid.NewGuid()
    area.LastUpdatedBy = "Jimmy"
    area.CreatedBy = "Jimmy"
    area.Name = areaName
    db.Area.AddObject(area)
    db.SaveChanges()

    Dim categoryExists As Boolean = False

    If (db.Area.Where(Function(x) x.LocalId = area.LocalId).Count > 1) Then
        categoryExists = True
    End If


End Sub

我可以确认我的配置文件没有被正确看到,我试图运行一个单元测试检查配置文件中的连接字符串的数量,当它应该是3时它出现了1。与Linq2SQL不同?我编写了测试并将它们与.dbml文件一起使用,这些连接存储在配置中,为什么不能看到EF?

编辑最新内容:

我跑了:

Dim configPath As String = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

查看我的配置文件,但在查看所有设置信息后,我有以下统计信息:

    ApplicationBase "C:\Users\Me\documents\visual studio 2010\Projects\LcmsEfTest\LcmsEfTest\bin"   String
    ApplicationBaseKey  "APPBASE"   String
    ApplicationName "domain-nunit.tdnet.dll"    String
    ApplicationNameKey  "APP_NAME"  String
    ApplicationTrust    Nothing System.Security.Policy.ApplicationTrust
    CachePath   "C:\Users\Me\AppData\Local\Temp\TestDrivenShadowCopy\634238699362254101"    String
    CachePathKey    "CACHE_BASE"    String
    ConfigurationExtension  ".config"   String
    ConfigurationFile   "C:\Users\Me\AppData\Local\Temp\tmp7A01.tmp"    String
    ConfigurationFileInternal   "C:\Users\Me\AppData\Local\Temp\tmp7A01.tmp"    String
    ConfigurationFileKey    "APP_CONFIG_FILE"   String
    DeveloperPath   Nothing String
    DeveloperPathKey    "DEV_PATH"  String
    DisallowAppBaseProbingKey   "DISALLOW_APP_BASE_PROBING" String
    DisallowApplicationBaseProbing  False   Boolean
    DisallowBindingRedirects    False   Boolean
    DisallowBindingRedirectsKey "DISALLOW_APP_REDIRECTS"    String
    DisallowCodeDownload    False   Boolean
    DisallowCodeDownloadKey "CODE_DOWNLOAD_DISABLED"    String
    DisallowPublisherPolicy False   Boolean
    DisallowPublisherPolicyKey  "DISALLOW_APP"  String
    DynamicBase Nothing String
    DynamicBaseKey  "DYNAMIC_BASE"  String
    HostBindingKey  "HOST_CONFIG"   String
    LicenseFile Nothing String
    LoaderOptimization  DomainMask {3}  System.LoaderOptimization
    LoaderOptimizationKey   "LOADER_OPTIMIZATION"   String
    MachineConfigKey    "MACHINE_CONFIG"    String
    PartialTrustVisibleAssemblies   Nothing String()
    PrivateBinPath  Nothing String
    PrivateBinPathEnvironmentVariable   "RELPATH"   String
    PrivateBinPathKey   "PRIVATE_BINPATH"   String
    PrivateBinPathProbe Nothing String
    PrivateBinPathProbeKey  "BINPATH_PROBE_ONLY"    String
    RuntimeConfigurationFile    "config\machine.config" String
    SandboxInterop  False   Boolean
    ShadowCopyDirectories   Nothing String
    ShadowCopyDirectoriesKey    "SHADOW_COPY_DIRS"  String
    ShadowCopyFiles Nothing String
    ShadowCopyFilesKey  "FORCE_CACHE_INSTALL"   String

- 值{Length = 18} String()

现在我尝试从我的Linq2SQL项目中获取相同的信息,这项工作正常,设置信息包含:

        ApplicationBase "C:\Users\Me\Documents\Visual Studio 2008\Projects\LCFVB\LCFVBTests\bin\Release"    String
    ApplicationName "domain-nunit.tdnet.dll"    String
    ApplicationTrust    Nothing System.Security.Policy.ApplicationTrust
    CachePath   "C:\Users\Me\AppData\Local\Temp\TestDrivenShadowCopy\634238707809841384"    String
    ConfigurationFile   "C:\Users\Me\AppData\Local\Temp\tmp5E19.tmp"    String
    DisallowApplicationBaseProbing  False   Boolean
    DisallowBindingRedirects    False   Boolean
    DisallowCodeDownload    False   Boolean
    DisallowPublisherPolicy False   Boolean
    DynamicBase Nothing String
    LicenseFile Nothing String
    LoaderOptimization  MultiDomainHost {3} System.LoaderOptimization
    PrivateBinPath  Nothing String
    PrivateBinPathProbe Nothing String
    SandboxInterop  False   Boolean
    ShadowCopyDirectories   Nothing String
    ShadowCopyFiles Nothing String

任何人都可以看到任何差异,可以说明为什么Nunit /测试驱动可以在配置中读取我的Linq2SQL连接信息,但不能用于实体框架?

2 个答案:

答案 0 :(得分:3)

这只是因为你正在使用NUnit而NUnit NOT 读取配置文件! 当然,在运行测试时,您正在运行 NUnit 应用程序,并且使用的配置文件是nunit-console.exe.confignunit-gui.exe.config

因此,您需要使用实体连接字符串更新Nunit Config文件,以便Nunit在运行单元测试时找到它。

有关详细信息,请查看How NUnit Finds Config Files

这是通过AppDomainSetup.ConfigurationFile获取活动配置文件路径的方法:

 string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

答案 1 :(得分:1)

试试这个:add name =“LCFEntities”connectionString =“metadata = res:// *; provider = System.Data.SqlClient; provider connection string ='Data Source = xxxxx; Initial Catalog = xxx; Persist Security Info = True;用户ID = LCF_admin;密码= xxxx; MultipleActiveResultSets = True'“providerName =”System.Data.EntityClient“/&gt;