views:

69

answers:

3

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:\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?

+1  A: 

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"/>

franklins
No dice (yes I replaced xxx with correct info).
sah302
+1  A: 
Morteza Manavi
While I am reading that and see it's apparently an issue, I am not sure that applies to me. I am also using testdriven.net to run tests right in the IDE, but I have never had this problem before. I have another project using the same tools, and have no problem testing using Linq2SQL (which stores connection strings in the .config). Everything works fine and I've never edited the Nunit Config File.
sah302
Then use `System.Configuration.ConfigurationManager` from within your test method to examine the content of the web.config your Nunit is using. You should be able to see the connection string. However, I still think my solution worth a try.
Morteza Manavi
It works, I did a count check (I have 3 connection strings) on the connection strings and it was able to see them.
sah302
Actually you are right, I am dummy and wrote the wrong test. The connectionString count is 1 not 3. But where do I find these nunit config files? I can't find them anywhere.
sah302
Ok, it depends on your NUnit setup, usually in the same bin folder where the NUnit DLL files are located. I update my answer with a way to get the currently active config file path. Please have a look.
Morteza Manavi
Okay I checked this information..some statistics: ConfigurationFile and ConfigurationFileInternal are `C:\Users\Me\AppData\Local\Temp\tmp7A01.tmp` , CachePath is `C:\Users\Me\AppData\Local\Temp\TestDrivenShadowCopy\634238699362254101` ConfigurationExtension is `config`. See my edit for full infos. What does this tell me?
sah302
A: 

create a config file for your unit test project (not the other projects) and add the connection string to it, first do it and then post a comment, as others said, you just have one active project and config file will be loaded from your active project, if you using unit test, the unit test project is your active project (during test) so connection string should be in its config file.

SaeedAlg
Did this same result.
sah302