.net 实体框架4.0,Can';找不到连接名

.net 实体框架4.0,Can';找不到连接名,.net,entity-framework,entity-framework-4,connection-string,.net,Entity Framework,Entity Framework 4,Connection String,我试图在一个项目中使用N-unit运行单元测试。项目中有.edmx文件,因此我看到很多人都认为这不是一个多项目问题。我保留了工具自动生成的所有默认值。My Web.config文件在中似乎包含连接字符串: 有人有线索吗 编辑: 这是在线路Dim db as New LCFEntities()上失败的测试。: 查看我的配置文件,但查看所有设置信息后,我有以下统计信息: ApplicationBase "C:\Users\Me\documents\visual studio 2010\Pro

我试图在一个项目中使用N-unit运行单元测试。项目中有.edmx文件,因此我看到很多人都认为这不是一个多项目问题。我保留了工具自动生成的所有默认值。My Web.config文件在
中似乎包含连接字符串:

有人有线索吗

编辑:

这是在线路
Dim db as New LCFEntities()上失败的测试。

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

    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}字符串()

现在,我尝试从我的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/test-driven为什么可以在配置中读取我的Linq2SQL连接信息,但不能在实体框架中读取?请尝试以下操作:add name=“LCFEntities”connectionString=“元数据=res://*;provider=System.Data.SqlClient;提供程序连接字符串=”数据源=xxxxx;初始目录=xxx;持久安全信息=True;用户ID=LCF_管理员;密码=xxxx;MultipleActiveResultSets=True''providerName=“System.Data.EntityClient”/>

尝试以下操作:add name=“LCFEntities”connectionString=“metadata=res://*;provider=System.Data.SqlClient;提供程序连接字符串=”数据源=xxxxx;初始目录=xxx;持久安全信息=True;用户ID=LCF_管理员;密码=xxxx;MultipleActiveResultSets=True''providerName=“System.Data.EntityClient”/>

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


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

有关更多信息,请查看。

这是如何通过以下方式获取活动配置文件的路径:
string configPath=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

这只是因为您使用的是NUnit,NUnit不读取配置文件! 当然,当您运行测试时,您运行的是NUnit应用程序,使用的配置文件是
NUnit console.exe.config
NUnit gui.exe.config


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

有关更多信息,请查看。

这是如何通过以下方式获取活动配置文件的路径:
string configPath=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

您收到的错误是什么?在配置中找不到指定的命名连接,或者指定的命名连接不打算与EntityClient提供程序一起使用,或者指定的命名连接无效。可能已在此处解决:您收到的错误是什么?指定的命名连接在配置中找不到,或者不打算使用使用EntityClient提供程序,或者无效。也许这已经在这里解决了:当我阅读并发现这显然是一个问题时,我不确定这是否适用于我。我还使用testdriven.net在IDE中运行测试,但我以前从未遇到过此问题。我有另一个项目使用相同的工具,并且使用测试没有问题Linq2SQL(将连接字符串存储在.config中)。一切正常,我从未编辑过Nunit配置文件。然后使用测试方法中的
System.Configuration.ConfigurationManager
检查Nunit正在使用的web.Config的内容。您应该能够看到连接字符串。但是,我仍然认为我的解决方案值得一试。它有效,我进行了计数检查(我有3个连接字符串)在连接字符串上,它可以看到它们。实际上你是对的,我是虚拟的,编写了错误的测试。connectionString计数是1而不是3。但是我在哪里找到这些nunit配置文件?我在任何地方都找不到。好的,这取决于nunit设置,通常在nunit DLL文件所在的同一个bin文件夹中。我更新我的answer提供了一种获取当前活动配置文件路径的方法。请看一看。当我阅读并发现这显然是一个问题时,我不确定这是否适用于我。我还使用testdriven.net在IDE中运行测试,但我以前从未遇到过此问题。我有另一个项目使用相同的工具,测试没有问题使用Linq2SQL(将连接字符串存储在.config中)。一切正常,我从未编辑过Nunit配置文件。然后使用测试方法中的
System.Configuration.ConfigurationManager
检查Nunit正在使用的web.Config的内容。您应该能够看到连接字符串。但是,我仍然认为我的解决方案值得一试。它有效,我进行了计数检查(我有3个连接字符串)在连接字符串上,它可以看到它们。实际上你是对的,我是虚拟的,编写了错误的测试。connectionString计数是1而不是3。但是我在哪里找到这些nunit配置文件?我在任何地方都找不到。好的,这取决于nunit设置,通常在nunit DLL文件所在的同一个bin文件夹中。我更新我的answer提供了获取当前活动配置文件路径的方法。请查看。
The specified named connection is either not found in the configuration, not intended         
to be used with the EntityClient provider, or not valid.
<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
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
        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
 string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;