开发者

Entity Framework 4.0, Can't find connection name

I am trying to run a unit test with N-unit in a project. The project has the .edmx file in it, so it's not a multi-project problem that I see a lot of people. I kept all the defaults from what was auto generated by the Tool. My Web.config file appears to have the connection string in the <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>

And in the LCF.designer.vb it looks to be looking for the correct name:

 ''' <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

And it errors out on this line Dim db As New LCFEntities(). The project is a web application project and I created a directory for the solution. Not sure if that matters.

The error is:

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

Anyone have any clue?

Edit:

This is the test that fails on line 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

I can confirm that my config files aren't being seen properly, I tried to run a unit test checking the count of connection strings in the config file, it came up 1 when it should have been 3. Is EF built that differently from Linq2SQL? I wrote tests and used them fine with a .dbml file and those connections are stored in config, why can't the EF ones be seen?

Edit Latest:

I ran:

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

to see my configuration file, but upon looking at all the setup information I have the following stats:

    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:开发者_Python百科\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

- Value {Length=18} String()

Now I tried getting this same information from my Linq2SQL project where this works fine and the setup information contains:

        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

Can anyone see any difference that would give any indication as to the difference for why Nunit/test driven can read my Linq2SQL connection info in config, but not for Entity Framework?


That's just because you are using NUnit and NUnit does NOT read config files! Of course, when you are running your tests, you are running the NUnit application and the config file used is either nunit-console.exe.config or nunit-gui.exe.config.

Therefore you need to update your Nunit Config file with the Entity Connection String so that Nunit will find it when you run your Unit Tests.

For more info have a look at How NUnit Finds Config Files.

This is how you can get the path of active Config file via AppDomainSetup.ConfigurationFile:

 string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;


Try this: add name="LCFEntities" connectionString="metadata=res://*;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"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜